Check if a Variable is Defined or Initialized in JavaScript

avatar

Borislav Hadzhiev

Wed Oct 06 20212 min read

Check if a Variable is Defined or Initialized #

To check if a variable is defined or initialized:

  1. Use the typeof operator, which returns a string indicating the type of the variable.
  2. If the type of the variable is not equal to the string 'undefined', then the variable has been initialized.
  3. E.g., if (typeof a !== 'undefined'), then the variable is defined.
index.js
let a; console.log(typeof a); // ๐Ÿ‘‰๏ธ 'undefined' // ๐Ÿ‘‡๏ธ the variable `b` is not declared in our code // but typeof doesn't throw an error console.log(typeof b); // ๐Ÿ‘‰๏ธ 'undefined' if (typeof a !== 'undefined') { // if this runs // ๐Ÿ‘‰๏ธ the variable has been declared }

In the code snippet we use the typeof operator to check if a variable's type is not equal to the string undefined. If the code block runs, we can be sure that the variable has been initialized.

Note that the typeof operator did not throw an error even when we checked for the type of a variable named b, which is not declared in our code.

However, there is a scenario where the typeof operator would throw an error.

If we use the typeof operator before declaring a variable with let or const, we would get an error.

index.js
// ๐Ÿ‘‡๏ธ Throws an error // Cannot access 'a' before initialization if (typeof a !== 'undefined') { } let a;

In the code snippet we check if the variable a has been defined, before actually initializing it with the let keyword.

This causes an error, because JavaScript assumes that we are trying to access a variable before initializing it.

The outcome would be the same if we defined the a variable with the const keyword.

However, the error would not occur if we declared the variable using the var keyword, because of how hoisting works in JavaScript.

index.js
// ๐Ÿ‘‡๏ธ no error occurs if (typeof a !== 'undefined') { } var a = 10;
The reason the typeof operator does not throw an error in this case, is because, the variable a gets hoisted to the top and its value set toundefined.

The code block above is the same as:

index.js
var a; if (typeof a !== 'undefined') { } a = 10;

The variable a is declared on line 1, however its value is set later on in our code, because of how hoisting works for variables in JavaScript.

You can also check if an already declared variable is set to a truthy value with a simple if statement.

index.js
let a; if (a) { // this code runs if // the `a` variable is NOT // ๐Ÿ‘‰๏ธ 0, empty string, null, undefined, false or NaN }

The check above only works if the a variable is declared and checks if the value stored in the variable is truthy.

If the variable is truthy the code in the if block runs.

All values in JavaScript are truthy, unless they are falsy. The falsy values are:

  • false, 0, "", null, undefined, NaN

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee