Require or disallow initialization in variable declarations
This rule is currently frozen and is not accepting feature requests.
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.
let foo = 1;
let bar;
if (foo) {
bar = 1;
} else {
bar = 2;
}
This rule is aimed at enforcing or eliminating variable initializations during declaration. For example, in the following code, foo is initialized during declaration, while bar is not.
let foo = 1;
let bar;
bar = 2;
This rule aims to bring consistency to variable initializations and declarations.
The rule takes two options:
"always" (the default), to enforce initialization at declaration, or "never" to disallow initialization during declaration. This rule applies to var, let, const, using, and await using variables, however "never" is ignored for const, using, and await using variables, as not initializing these variables would generate a parse error.ignoreForLoopInit, which indicates if initialization at declaration is allowed in for loops when "never" is set, since it is a very typical use case.You can configure the rule as follows:
Variables must be initialized at declaration (default)
{
"init-declarations": ["error", "always"],
}
Variables must not be initialized at declaration
{
"init-declarations": ["error", "never"]
}
Variables must not be initialized at declaration, except in for loops, where it is allowed
{
"init-declarations": ["error", "never", { "ignoreForLoopInit": true }]
}
Examples of incorrect code for the default "always" option:
/*eslint init-declarations: ["error", "always"]*/
function foo() {
var ;
let ;
}
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;
using quux = getSomething();
}
async function foobar() {
await using quux = getSomething();
}
Examples of incorrect code for the "never" option:
/*eslint init-declarations: ["error", "never"]*/
function foo() {
var ;
let ;
for (let ; i < 1; i++) {}
}
Examples of correct code for the "never" option:
/*eslint init-declarations: ["error", "never"]*/
function foo() {
var bar;
let baz;
const buzz = 1;
using quux = getSomething();
}
async function foobar() {
await using quux = getSomething();
}
The "never" option ignores const, using, and await using variable initializations.
Examples of correct code for the "never", { "ignoreForLoopInit": true } options:
/*eslint init-declarations: ["error", "never", { "ignoreForLoopInit": true }]*/
for (let i = 0; i < 1; i++) {}
This rule additionally supports TypeScript type syntax.
Examples of incorrect TypeScript code for this rule:
/* eslint init-declarations: ["error", "never"] */
let ;
const class1 = class NAME {
constructor() {
var ;
}
};
namespace myLib {
let ;
}
/* eslint init-declarations: ["error", "always"] */
namespace myLib {
let ;
}
namespace myLib1 {
const ;
namespace myLib2 {
let ;
namespace myLib3 {
let ;
}
}
}
Examples of correct TypeScript code for this rule:
/* eslint init-declarations: ["error", "never"] */
declare const foo: number;
declare namespace myLib {
let numberOfGreetings: number;
}
interface GreetingSettings {
greeting: string;
duration?: number;
color?: string;
}
/* eslint init-declarations: ["error", "always"] */
declare const foo: number;
declare namespace myLib {
let numberOfGreetings: number;
}
interface GreetingSettings {
greeting: string;
duration?: number;
color?: string;
}
When you are indifferent as to how your variables are initialized.
This rule was introduced in ESLint v1.0.0-rc-1.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/init-declarations