Home > Article > Web Front-end > Analysis of the difference between Null and Undefined in JavaScript_javascript skills
There are two primitive types in JavaScript: Null and Undefined. These two types often make JavaScript developers confused, when is it Null and when is it Undefined?
The Undefined type has only one value, which is undefined. When a declared variable has not been initialized, the variable's default value is undefined.
The Null type also has only one value, which is null. Null is used to represent an object that does not yet exist. It is often used to indicate that a function attempts to return a non-existent object.
This code displays as true, which means that the value of oVlaue is undefined because we have not initialized it.
When the DOM node with the id "notExistElement" does not exist on the page, this code displays "true" because we are trying to get an object that does not exist.
The first line of code is easy to understand, the type of undefined is Undefined; the second line of code makes people confused, why is the type of null again Object? In fact, this was a mistake in the initial implementation of JavaScript, which was later adopted by ECMAScript. Today we can explain that null is a placeholder for a non-existent object, but we still need to pay attention to this feature when actually coding.