Home  >  Article  >  Web Front-end  >  When is a Boolean value considered false in JavaScript?

When is a Boolean value considered false in JavaScript?

WBOY
WBOYOriginal
2022-07-01 15:29:092785browse

The situation when the Boolean value is false in JavaScript: 1. When the data value is "undefined", it means that it is not defined and is considered false; 2. null, which means that the empty value is considered false; 3. The number "0", If it is a string of 0, it is regarded as true; 4. NaN, which means that the result cannot be calculated, is regarded as false; 5. An empty string is regarded as false; 6. A Boolean value of false is regarded as false.

When is a Boolean value considered false in JavaScript?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

When is a Boolean value considered false in JavaScript

The Boolean value is a logical value, only true and false (true and false).

Six situations in which Boolean values ​​in js are false

The following 6 values ​​are false when converted into Boolean values, and other conversions are true

1, undefined (undefined Definition, appears when the value cannot be found)

2, null (represents a null value)

3, false (false for Boolean value, string "false" Boolean value is true)

4, 0 (number 0, string "0" Boolean value is true)

5, NaN (occurs when the result cannot be calculated, indicating "non-numeric value"; but typeof NaN===" number")

6, "" (double quotes) or '' (single quotes) (empty string, it is also true when there are spaces in the middle)

Note that empty arrays, empty objects, negative values All other Boolean values ​​are true when passed to the conditional statement. All other values, including all objects, will evaluate to true when passed to the conditional statement.

Please do not confuse the original Boolean values ​​true and false with the true and false of Boolean objects. For example:

var b = new Boolean(false);
if (b) //结果视为真
if (b == true) // 结果视为假

Extended knowledge:

JavaScript data types

JavaScript variables can save a variety of data types: numerical values, string values, arrays, Objects, etc.:

var length = 7;                             // 数字
var lastName = "Gates";                      // 字符串
var cars = ["Porsche", "Volvo", "BMW"];         // 数组
var x = {firstName:"Bill", lastName:"Gates"};    // 对象

[Related recommendations:

javascript video tutorial

, web front-end]

The above is the detailed content of When is a Boolean value considered false in JavaScript?. 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