Many of you confuse Null and Undefined. However, there are differences and peculiarities that we are going to discuss in this tutorial.
Undefined is a variable in the global scope. The default value of undefined is the primitive value undefined. Undefined means a variable that has been declared, but the value of that variable has not yet been assigned.
For exmple, let’s assign the value of null to test:
let value1 = null; console.log(value1); // null
Typically, undefined means a variable that has been declared not defined:
let value2; console.log(value2); // undefined
From the above example, you already know that undefined is a type itself, while null is an object.
However, when checking for null or undefined, remember the differences between equality (==) and identity (===) operators, as equality performs type-conversion.
console.log(typeof null); // "object" console.log(typeof undefined); // "undefined" console.log(null === undefined); // false console.log(null == undefined );// true console.log(null === null ); // true console.log(null == null); // true console.log(!null ); // true