Null
: The valuenull
represents the intentional absence of any object value. It is explicitly set by the programmer to indicate that there is no value. For example:let x = null; console.log(x); // null
Undefined
: The valueundefined
is automatically assigned to variables that are declared but not initialized. For example:let x; console.log(x); // undefined
Booleans
: TheBooleans
data type is used to represent truth values (true
orfalse
). They are typically used in conditions, loops, and other control structures to make decisions in your code. For example:let x = true; let y = false;
- Difference between
null
andundefined
When checking fornull
orundefined
, beware of the differences between equality (==) and identity (===) operators (opens in a new tab), as the former performs type-conversion.typeof null; // "object" (not "null" for legacy reasons) typeof undefined; // "undefined" null === undefined; // false null == undefined; // true null === null; // true null == null; // true !null; // true Number.isNaN(1 + null); // false Number.isNaN(1 + undefined); // true