Many novices find it difficult to understand null and undefined in the process of learning front-end, especially in the javacript language. In fact, as long as we stand in the perspective of language history development or the author’s design, the problem becomes simple.

JavaScript history

When JavaScript was born in 1995, it initially set only null as a value for “none”, just like Java. In the tradition of C, NULL is designed to be automatically converted to 0.

However, Brendan Eich, the designer of JavaScript, feels that this is not enough, mainly because JavaScript data types are divided into primitive and complex types (or primitive and reference types). Null, like in the Java language, is treated as an object, and something is needed to indicate the primitive type of “nothing”.

Author Brendan Eich felt that the value representing this “nothing” had better not be an object. Second, the earliest versions of JavaScript did not include error handling, and when a data type mismatch occurred, the type was automatically converted or silently failed. Brendan Eich felt that if null was automatically converted to 0, it would be hard to spot errors. Therefore, Brendan Eich designed a undefined, undefined type with only one value, namely the special undefined

conclusion

Null means “no object”. A null value means an empty object pointer. For example, to declare a variable of a reference type, var obj = null can be used

Otherwise, use undefined, default undefined, such as var a;