Loading course content...
Loading course content...
Code Playground is only enabled on larger screen sizes.
undefined and null are two very special values in JavaScript. They are both of its own data type, and they both indicate the lack of something meaningful.
In most cases, you can treat them as the same thing.
When you apply the typeof operator on undefined, it returns undefined, which means undefined is of its own data type.
typeof undefined;undefined represents the absence of a meaningful value. When a variable is initialized without a value, it will be assigned undefined automatically.
null, on the other hand, defines an empty value. Unlike undefined, the value exists, it is just empty.
In practice, an empty value and an absence of value are essentially the same thing.
The fact that there are two values defining similar things is just a mistake in JavaScript's design. In most cases, you can see them as interchangeable.
In fact, when you compare them, JavaScript will tell you they are the same.
undefined == null;But when you use === (equal value and equal type), JavaScript will return false.
undefined === null;That's because they are both unique and of different data types.
However, when you apply the typeof operator on null. JavaScript will tell you it is an object.
typeof null; // -> objectThis is actually another mistake in JavaScript. null is of type null, not an object.
The only reason that JavaScript hasn't fixed these mistakes is that it might break some older software.
console.log(typeof undefined);