eslint/init-declarations Style
What it does
Require or disallow initialization in variable declarations
Why is this bad?
In JavaScript, variables can be assigned during declaration, or at any point afterwards using an assignment statement. For example, in the following code, foo is initialized during declaration, while bar is initialized later.
Examples
var foo = 1; var bar; if (foo) { bar = 1; } else { bar = 2; }
Examples of incorrect code for the default "always" option:
/*eslint init-declarations: ["error", "always"]*/
function foo() {
var bar;
let baz;
}Examples of incorrect code for the "never" option:
/*eslint init-declarations: ["error", "never"]*/
function foo() {
var bar = 1;
let baz = 2;
for (var i = 0; i < 1; i++) {}
}Examples of correct code for the default "always" option:
/*eslint init-declarations: ["error", "always"]*/
function foo() {
var bar = 1;
let baz = 2;
const qux = 3;
}Examples of correct code for the "never" option:
/*eslint init-declarations: ["error", "never"]*/
function foo() {
var bar;
let baz;
const buzz = 1;
}Examples of correct code for the "never", { "ignoreForLoopInit": true } options:
/*eslint init-declarations: ["error", "never", { "ignoreForLoopInit": true }]*/
for (var i = 0; i < 1; i++) {}Configuration
This rule accepts a configuration object with the following properties:
ignoreForLoopInit
type: boolean
default: false
When set to true, allows uninitialized variables in the init expression of for, for-in, and for-of loops. Only applies when mode is set to "never".
mode
type: "always" | "never"
When set to "always" (default), requires that variables be initialized on declaration. When set to "never", disallows initialization during declaration.
How to use
To enable this rule in the CLI or using the config file, you can use:
oxlint --deny init-declarations{
"rules": {
"init-declarations": "error"
}
}