src/content/docs/linter/rules/no-useless-undefined-initialization.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.7.2` - Diagnostic Category: [`lint/complexity/noUselessUndefinedInitialization`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-undef-init`](https://eslint.org/docs/latest/rules/no-undef-init){
"linter": {
"rules": {
"complexity": {
"noUselessUndefinedInitialization": "error"
}
}
}
}
Disallow initializing variables to undefined.
A variable that is declared and not initialized to any value automatically gets the value of undefined.
It’s considered a best practice to avoid initializing variables to undefined.
Please note that any inline comments attached to the initialization value or variable will be moved at the end of the variable declaration on auto-fix. Please be also aware that this differs from Eslint's behaviour.
var a = undefined;
let b = undefined, c = 1, d = 2;
for (let i = 0; i < 100; i++) {
let i = undefined;
}
let f = /**/undefined/**/ ;
var a = 1;
class Foo {
bar = undefined;
}
Exported variables are not flagged because in some frameworks (e.g., Svelte 4),
initializing exported variables to undefined is used to declare optional props.
export let x = undefined;
let y = undefined;
export { y };