0%

undefined vs not defined

undefined vs not defined

In JavaScript, undefined and not defined are two different concepts.

  • undefined: a variable has been declared but has not yet been assigned a value.
  • not defined: a variable has not been declared(not exists).

undefined

1
2
let a;
console.log(a); // undefined

not defined

1
console.log(b); // Uncaught ReferenceError: b is not defined

Whenever you try to access a variable that is not declared, JavaScript throws an error: Uncaught ReferenceError: xxx is not defined. This is because variable b is not declared anywhere in the code. but you can still use typeof to check if a variable is defined or not.

typeof

1
2
3
let a;
console.log(typeof a); // undefined
console.log(typeof b); // undefined, even b is not defined