Home  >  Article  >  Web Front-end  >  Does javascript have null values?

Does javascript have null values?

青灯夜游
青灯夜游Original
2022-01-18 14:31:171674browse

There are five types of null values ​​in javaScript, namely "false", "null", "undefined", """" and "0"; their corresponding data types are "boolean" and "object" ", "undefined", "String", "number".

Does javascript have null values?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

There are five types of null and false values ​​in javaScript, namely false, null, undefined, "", and 0. In a broad sense, these five values ​​are invalid or null values ​​of the corresponding data type.

What these five values ​​have in common is that the false branch will be executed when the if statement is executed, and the true branch will be executed when the corresponding non-statement is executed.

1, undefined

In javaScript, undefined is derived from null, and the browser default variable is undefined when the variable is initialized. Code example:

var str;
alert(str);//undefined

Also

if(undefined == null) {
  alert("undefined == null 为true");
}

Popup: undefined == null is true

2, null

null represents a non-existent object, code example:

var obj  = document.getElementById("btn");
alert(obj);//null

Although the type of null is object, null does not have the characteristics of an object. We cannot perform default calls of object instances such as null.toString(), null.constructor, etc. The execution result of

null ==undefined is true. In addition, assign the variable to null to facilitate garbage collection.

3,"",0,false

"",0,false They appear as false values ​​in the if statement, but they are all meaningful data and are just used as null or false values.

"".toString(), (0).toString(), false.toString() are all legal and executable statement.

In addition,

if(0 == []){
   alert("0 ==[] 为true");
} 
if(0 == ''){
   alert("0 =='' 为true");
} 
if(0 ==false){
   alert("0 ==false 为true");
}

the browser pops up in turn:

0 ==[] 为true
0 =='' 为true
0 ==false 为true

and

if("" == false){
   alert(‘“”== false 为true’);
}

the browser pops up:

“”== false 为true

js五Type of null value:

  • typeof(undefined) == 'undefined'

  • typeof(null) == 'object '

  • typeof("") == 'String'

  • typeof(0) == 'number'

  • typeof(false) == 'boolean'

[Related recommendations: javascript learning tutorial]

The above is the detailed content of Does javascript have null values?. 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