DATA TYPES
There are six simple data types (also called primitive types) in ECMAScript: Undefined, Null, Boolean, Number, String, and Symbol. There is also one complex data type called Object, which is an unordered list of name–value pairs. ECMAScript’s data types have dynamic aspects that make each single data type behave like several.
Undefined
The Undefined type has only one value, which is the special value undefined. When a variable is declared using var or let but not initialized, it is assigned the value of undefined as follows:
let message;
console.log(message == undefined); // true
In this example, the variable message is declared without initializing it. When compared with the literal value of undefined
, the two are equal. This example is identical to the following:
let message = undefined;
console.log(message == undefined); // true
Here the variable message is explicitly initialized to be undefined. This is unnecessary because, by default, any uninitialized variable gets the value of undefined
.
Null
The Null type is the second data type that has only one value: the special value null
. Logically, a null
value is an empty object pointer, which is why typeof returns "object" when it’s passed a null
value in the following example:
let car = null;
console.log(typeof car); // "object"
When defining a variable that is meant to later hold an object, it is advisable to initialize the variable
to null
as opposed to anything else. That way, you can explicitly check for the value null
to determine if the variable has been filled with an object reference at a later time, such as in this example:
if (car != null) {
// do something with car
}
Even though null
and undefined
are related, they have very different uses. As mentioned previously, you should never explicitly set the value of a variable to undefined
, but the same does not hold true for null
. Any time an object is expected but is not available, null
should be used in its place. This helps to keep the paradigm of null
as an empty object pointer and further differentiates it from undefined
.
Top comments (1)
You've missed
BigInt
- developer.mozilla.org/en-US/docs/W...