Home > Web Front-end > JS Tutorial > body text

What to say about null and false values ​​in javaScript

亚连
Release: 2018-06-20 11:53:44
Original
1327 people have browsed it

JavaScript is the most popular programming language in the world. There are five types of empty values ​​and false values ​​in javaScript, namely false, null, undefined, "", and 0. Broadly speaking, these five values ​​​​are invalid or null values ​​​​of the corresponding data types

JavaScript is the most popular programming language in the world. This language can be used in HTML and the web, and can be used on a wide range of devices such as servers, PCs, laptops, tablets, and smartphones.

JavaScript is a scripting language
JavaScript is a lightweight programming language.
JavaScript is programming code that can be inserted into HTML pages.
JavaScript, when inserted into an HTML page, can be executed by all modern browsers.
JavaScript is easy to learn.

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

3 "",0,false

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

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

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
if("" == false){
  alert(‘“”== false 为true');
}
Copy after login

The browser pops up:

""== false is true

js Five types of null values:

typeof(undefined) == 'undefined'
typeof(null) == 'object'
typeof("") == 'String'
typeof(0) == 'number'
typeof(false) == 'boolean'
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to implement the web version of the calculator in JS

How to implement the parabolic trajectory of the ball using JS

How to implement binary tree traversal using JavaScript

How to implement cookie cross-domain in axios

The above is the detailed content of What to say about null and false values ​​in javaScript. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template