Introduction to null, undefined, and nullundefined in JavaScript
Null indicates a special value, which is often used to describe "null ". Perform the typeof operation on null, and the result returns the string "object". That is to say, null can be considered as a special object value, meaning "non-object" (strange feeling ). In fact, null is generally considered to be the only member of its own type. It can indicate that numbers, strings, and objects are "valueless.
JavaScript also has a second value to indicate the vacancy of the value, that is, undefined. The undefined value is used to indicate a deeper "null ". When undefined is declared as a variable but not initialized, ② when the object attribute to be queried or the element of the array does not exist, ③ if the function does not return any value, then, return the undefined ④ value of the function parameter that does not provide the real parameter, and only get undefined.
Similarities between the two: ① as mentioned above, they are all "false values", that is, when JavaScript expects to use a Boolean value, they will be converted to false; ② neither of them contains any attributes or methods.
Differences between the two: ① null is a keyword of the JavaScript language, while undefined is a predefined global variable of JavaScript, not a keyword. Moreover, in ECMAScript 3, undefined is a readable and writable variable that can be assigned any value. This error is corrected in ECMAScript 5, in this version, undefined is read-only (I can see that the current browser basically supports ECMAScript 5 on the Internet. I don't know why I didn't report an error when assigning a value to undefined in the browser, only does not change its value); ② executes the typeof operation, null returns the "object" string, and undefined returns the "undefined" string.
For comparison between null and undefined, null = undefined returns true, null = undefined returns false. It can be considered that undefined represents a system-level, unexpected, or similar false value vacancy, while null represents a program-level, normal, or expected value vacancy. If you want to assign values to variables or attributes or input functions as parameters, it is best to use null.
The above is all the content of this article. I hope you will like it.