JavaScript

JavaScript

Made by DeepSource

Variables should not be initialized to undefined JS-0126

Anti-pattern
Minor

In JavaScript, a variable that is declared and not initialized to any value automatically gets the value of undefined. For example:

var foo;

console.log(foo === undefined);     // true

It's therefore unnecessary to initialize a variable to undefined, such as:

var foo = undefined;

It's considered a best practice to avoid initializing variables to undefined.

Bad Practice

var foo = undefined;
let bar = undefined;

Recommended

var foo;
let bar;
const baz = undefined;