Does javascript have null values?

青灯夜游
Release: 2022-01-18 14:31:17
Original
1667 people have browsed it

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
Copy after login

Also

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

Popup:undefined == null is true

2, null

null represents a non-existent object, code example:

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

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 ==undefinedis true. In addition, assign the variable to null to facilitate garbage collection.

3,"",0,false

"",0,falseThey 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"); }
Copy after login

the browser pops up in turn:

0 ==[] 为true 0 =='' 为true 0 ==false 为true
Copy after login

and

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

the browser pops up:

“”== false 为true
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!