The Difference Between Null and Undefined in JavaScript
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.
Null is of the JavaScript primitive values and represents the intentional absence of object value. That is to say, it can be assigned to a variable as a representation of no value.
For exmple, let’s assign the value of null to test:
Typically, undefined means a variable that has been declared not defined:
From the above example, you already know that undefined is a type itself, while null is an object.
But why the typeof operator returns “object” for a null value? It was an error in the original JavaScript implementation that was then copied in ECMAScript. Today, null is considered a placeholder for an object, despite the fact of technically being a primitive value.
However, when checking for null or undefined, remember the differences between equality (==) and identity (===) operators, as equality performs type-conversion.
To sum up, in JavaScript null means "nothing" and “empty”, something that does not exist. And, a variable without a value called undefined.