English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Conversion of Boolean Values in JavaScript and Detailed Explanation of "&&", "||", and "!!"

Introduction

First of all, it is necessary to know that in JavaScript there is6The values that are false are: 0, '', null, undefined, NaN, and false, others (including {}, [], Infinity) are true.

Boolean() function or double negation can be used to obtain the boolean value of an object, for example, Boolean(undefined) and !!undefined can also obtain the boolean value false,

For 0, '', null, undefined, NaN, {}, [], Infinity, the boolean values are respectively false, false, false, false, false, true, true, true.

Therefore, one thing we know is that the boolean value of an object is true, even if it is an empty object {}.

bool value conversion

Data type bool value conversion
undefined undefined is converted to false
Object null is converted to false, others are true
Boolean false is converted to false, true is converted to true
Number 0, NaN is converted to false, others are true
String "" is converted to false, others are true

"&&"

The rules for the && operator in JavaScript are as follows:

If the value of the expression on the left side of && is true, then return the value of the expression on the right side; otherwise, return the value of the expression on the left side. When multiple && expressions are evaluated together, the value of the first expression that evaluates to false is returned, and if all expressions evaluate to true, the value of the last expression evaluated is returned.

const aa = {'name': 'xx'};
const bb = aa && aa.age; // bb output is undefined;
let cc;
const dd = cc && cc.name &63; cc.name : undefined; // dd outputs undefined
const dd = cc && cc.name; // dd outputs undefined;

The execution results of the above two lines of code are the same. I have always used the above method to write code, but I found that some unit tests cannot be covered, resulting in a very low unit test branch coverage. The following method can solve this problem very well. The effect of these two is the same.

"||"

The calculation rules of the "||" operator in JavaScript are as follows:

If the value of the left expression of "||" is true, then return the value of the left expression; otherwise, return the value of the right expression. When multiple "||" expressions are calculated together, return the value of the first expression whose calculation result is true. If all expressions have a calculation result of false, return the value of the rightmost expression.

const aa = false || 'xx'; // aa outputs 'xx' 

"!!"

"!!" is used to force convert an expression to a boolean value, with the result being true or false.

const aa = 'xx';
const bb = !!aa; // bb outputs true
const cc = !!(NaN || undefined || null || 0 || '' ); // cc is false;

Summary

That's all for this article. I hope the content of this article has certain reference value for everyone's learning or work. If you have any questions, you can leave messages for communication. Thank you for your support of the Yell Tutorial.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @) and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like