8 Variables can be declared at any point in JavaScript code using `var`, `let`, or `const`. There are many styles and preferences related to the declaration of variables, and one of those is deciding on how many variable declarations should be allowed in a single function.
10 There are two schools of thought in this regard:
12 1. There should be just one variable declaration for all variables in the function. That declaration typically appears at the top of the function.
13 1. You should use one variable declaration for each variable you want to define.
18 // one variable declaration per function
23 // multiple variable declarations per function
30 The single-declaration school of thought is based in pre-ECMAScript 6 behaviors, where there was no such thing as block scope, only function scope. Since all `var` statements are hoisted to the top of the function anyway, some believe that declaring all variables in a single declaration at the top of the function removes confusion around scoping rules.
34 This rule enforces variables to be declared either together or separately per function ( for `var`) or block (for `let` and `const`) scope.
38 This rule has one option, which can be a string option or an object option.
42 * `"always"` (default) requires one variable declaration per scope
43 * `"never"` requires multiple variable declarations per scope
44 * `"consecutive"` allows multiple variable declarations per scope but requires consecutive variable declarations to be combined into a single declaration
48 * `"var": "always"` requires one `var` declaration per function
49 * `"var": "never"` requires multiple `var` declarations per function
50 * `"var": "consecutive"` requires consecutive `var` declarations to be a single declaration
51 * `"let": "always"` requires one `let` declaration per block
52 * `"let": "never"` requires multiple `let` declarations per block
53 * `"let": "consecutive"` requires consecutive `let` declarations to be a single declaration
54 * `"const": "always"` requires one `const` declaration per block
55 * `"const": "never"` requires multiple `const` declarations per block
56 * `"const": "consecutive"` requires consecutive `const` declarations to be a single declaration
57 * `"separateRequires": true` enforces `requires` to be separate from declarations
59 Alternate object option:
61 * `"initialized": "always"` requires one variable declaration for initialized variables per scope
62 * `"initialized": "never"` requires multiple variable declarations for initialized variables per scope
63 * `"initialized": "consecutive"` requires consecutive variable declarations for initialized variables to be a single declaration
64 * `"uninitialized": "always"` requires one variable declaration for uninitialized variables per scope
65 * `"uninitialized": "never"` requires multiple variable declarations for uninitialized variables per scope
66 * `"uninitialized": "consecutive"` requires consecutive variable declarations for uninitialized variables to be a single declaration
70 Examples of **incorrect** code for this rule with the default `"always"` option:
75 /*eslint one-var: ["error", "always"]*/
121 Examples of **correct** code for this rule with the default `"always"` option:
126 /*eslint one-var: ["error", "always"]*/
188 Examples of **incorrect** code for this rule with the `"never"` option:
193 /*eslint one-var: ["error", "never"]*/
226 Examples of **correct** code for this rule with the `"never"` option:
231 /*eslint one-var: ["error", "never"]*/
263 // declarations with multiple variables are allowed in for-loop initializers
264 for (var i = 0, len = arr.length; i < len; i++) {
273 Examples of **incorrect** code for this rule with the `"consecutive"` option:
278 /*eslint one-var: ["error", "consecutive"]*/
307 Examples of **correct** code for this rule with the `"consecutive"` option:
312 /*eslint one-var: ["error", "consecutive"]*/
342 ### var, let, and const
344 Examples of **incorrect** code for this rule with the `{ var: "always", let: "never", const: "never" }` option:
349 /*eslint one-var: ["error", { var: "always", let: "never", const: "never" }]*/
369 Examples of **correct** code for this rule with the `{ var: "always", let: "never", const: "never" }` option:
374 /*eslint one-var: ["error", { var: "always", let: "never", const: "never" }]*/
394 Examples of **incorrect** code for this rule with the `{ var: "never" }` option:
399 /*eslint one-var: ["error", { var: "never" }]*/
410 Examples of **correct** code for this rule with the `{ var: "never" }` option:
415 /*eslint one-var: ["error", { var: "never" }]*/
421 const bar = 1; // `const` and `let` declarations are ignored if they are not specified
430 Examples of **incorrect** code for this rule with the `{ separateRequires: true }` option:
435 /*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/
438 var foo = require("foo"),
444 Examples of **correct** code for this rule with the `{ separateRequires: true }` option:
449 /*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/
452 var foo = require("foo");
461 var foo = require("foo"),
462 bar = require("bar");
467 Examples of **incorrect** code for this rule with the `{ var: "never", let: "consecutive", const: "consecutive" }` option:
472 /*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive" }]*/
496 Examples of **correct** code for this rule with the `{ var: "never", let: "consecutive", const: "consecutive" }` option:
501 /*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive" }]*/
527 Examples of **incorrect** code for this rule with the `{ var: "consecutive" }` option:
532 /*eslint one-var: ["error", { var: "consecutive" }]*/
543 Examples of **correct** code for this rule with the `{ var: "consecutive" }` option:
548 /*eslint one-var: ["error", { var: "consecutive" }]*/
554 const c = 1; // `const` and `let` declarations are ignored if they are not specified
563 ### initialized and uninitialized
565 Examples of **incorrect** code for this rule with the `{ "initialized": "always", "uninitialized": "never" }` option:
570 /*eslint one-var: ["error", { "initialized": "always", "uninitialized": "never" }]*/
582 Examples of **correct** code for this rule with the `{ "initialized": "always", "uninitialized": "never" }` option:
587 /*eslint one-var: ["error", { "initialized": "always", "uninitialized": "never" }]*/
609 Examples of **incorrect** code for this rule with the `{ "initialized": "never" }` option:
614 /*eslint one-var: ["error", { "initialized": "never" }]*/
625 Examples of **correct** code for this rule with the `{ "initialized": "never" }` option:
630 /*eslint one-var: ["error", { "initialized": "never" }]*/
635 var a, b, c; // Uninitialized variables are ignored
641 Examples of **incorrect** code for this rule with the `{ "initialized": "consecutive", "uninitialized": "never" }` option:
646 /*eslint one-var: ["error", { "initialized": "consecutive", "uninitialized": "never" }]*/
660 Examples of **correct** code for this rule with the `{ "initialized": "consecutive", "uninitialized": "never" }` option:
665 /*eslint one-var: ["error", { "initialized": "consecutive", "uninitialized": "never" }]*/
679 Examples of **incorrect** code for this rule with the `{ "initialized": "consecutive" }` option:
684 /*eslint one-var: ["error", { "initialized": "consecutive" }]*/
699 Examples of **correct** code for this rule with the `{ "initialized": "consecutive" }` option:
704 /*eslint one-var: ["error", { "initialized": "consecutive" }]*/
721 * **JSHint**: This rule maps to the `onevar` JSHint rule, but allows `let` and `const` to be configured separately.
722 * **JSCS**: This rule roughly maps to [disallowMultipleVarDecl](https://jscs-dev.github.io/rule/disallowMultipleVarDecl).
723 * **JSCS**: This rule option `separateRequires` roughly maps to [requireMultipleVarDecl](https://jscs-dev.github.io/rule/requireMultipleVarDecl).