Home  >  Article  >  Web Front-end  >  What is javascript null

What is javascript null

藏色散人
藏色散人Original
2021-11-18 11:24:523953browse

Null is one of the basic data types of JavaScript. Null is used to indicate that the value of a variable is empty. We can clear the object by setting the value to null. The syntax is such as "var person = null;".

What is javascript null

The operating environment of this article: windows7 system, javascript version 1.8.5, DELL G3 computer

What is javascript null?

null is one of the basic data types of javascript. Null is used to indicate that the value of a variable is empty.

We can clear the object by setting the value to null:

such as

var person = null;           // 值是 null,但是类型仍然是对象

JavaScript data type

Value type (basic type):

String, Number, Boolean, Pair Null, Undefined, Symbol.

Reference data types:

Object (Object), Array (Array), Function (Function).

Note: Symbol is a new primitive data type introduced in ES6 to represent unique values.

Undefined and null

Undefined type values ​​have only one undefined, which is used to indicate that a variable does not exist or has not been assigned a value. It is also used to Indicates that the object's property does not exist. null is used to indicate that the value of the variable is empty. The difference between Undefined and null is subtle. Generally speaking, undefined means that no value is set for the variable or the attribute does not exist, while null means that the variable has a value, but its value is null.

But if there is no precise comparison, many times undefined and null themselves want to wait, that is, null==undefined will return true. If you want to accurately distinguish null and undefined, you should consider using the exact equal symbol (===)

<script type="text/javascript">
// 声明变量x , y
var x , y = null;
// 判断x的值是否为空
if (x === undefined) 
{
alert(&#39;声明变量后默认值为undefined&#39;);
}
if (x === null)
{
alert(&#39;声明变量后默认值为null&#39;);
}
// 判断x(其值为undefined)是否与y(其值为null)相等
if (x == y) 
{
alert("x(undefined)==y(null)");
}
// 测试一个并不存在的属性
if(String.xyz === undefined)
{
alert("不存在的属性值默认为undefined");
}
</script>

Recommended study: "javascript basic tutorial"

The above is the detailed content of What is javascript null. 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