Home  >  Article  >  Web Front-end  >  Detailed explanation of null and undefined in js

Detailed explanation of null and undefined in js

小云云
小云云Original
2018-03-19 16:47:322045browse

This article mainly shares the detailed explanation of null and undefined in js, including the differences between them. I hope it can help everyone.

1.null is a keyword in JavaScript, which represents a special value and is often used to describe "null value". The typeof data type is object, therefore, null can be considered as a special object. But in fact, null is usually considered to be the only member of its own type.

2. undefined is not a keyword, it indicates a vacancy in the value. It is a value of the variable, indicating that the variable has not been initialized. The typeof data type is undefined. In ECMAScript3, undefined was considered a read-write variable. This error was corrected in ECMAScript5 and changed to read-only.

3. Although null and undefined are different, they both represent "the vacancy of value". Use "==" to consider the two equal, and you need to use the strict equality operator "===" to distinguish them. When the expected value is Boolean, their values ​​are all false, that is, false. Both null and undefined are types that do not have methods.

4. Usage distinction: undefined is a vacancy that represents a system-level, unexpected error-like value. null is a vacancy representing a program-level, normal and expected value. When a function needs to be passed as a parameter, null is more appropriate.

Related recommendations:

What is the original design intention of null and undefined in js

The above is the detailed content of Detailed explanation of null and undefined in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn