This article mainly introduces to you the conversion method of bool value in js and the related information of "&&", "||", "!!". The article introduces it in detail through sample code, which is very helpful for everyone's learning. Or the work has certain reference and learning value. Friends who need it can come and take a look below.
Preface
The first thing you need to know is that there are 6 values in js that are false, namely: 0, '', null, undefined, NaN and false, others (including {}, [], Infinity) are true.
You can use the Boolean() function or negate twice to get the Boolean value of the object, such as Boolean(undefined ) and !!undefined can also obtain the Boolean value false,
For 0, '', null, undefined, NaN, {}, [], Infinity, the Boolean value is false false false false false true true true.
So one thing we know is: the Boolean value of an object is true, even if it is an object {}.
bool value conversion
bool value conversion | |
---|---|
undefined is converted to false | |
null is converted to false, Others are true | |
false is converted to false, true is converted to true | |
0, NaN is converted to false, others are true | |
"" is converted to false, others are true |
"&&"
The "&&" operator in JavaScript is as follows:
If && left If the value of the side expression is true, the value of the right side expression is returned; otherwise, the value of the left side expression is returned. When multiple && expressions are evaluated together, the value of the first expression that evaluates to false is returned. If all expressions evaluate to true, the value of the rightmost expression is returned.const aa = {'name': 'xx'}; const bb = aa && aa.age; // bb输出为undefined; let cc; const dd = cc && cc.name ? cc.name : undefined; // dd输出为undefined const dd = cc && cc.name; // dd输出为undefined;
"||"
The algorithm of "||" operator in javascript is as follows:
If the value of the expression on the left side of "||" is true, return the value of the expression on the left side; otherwise, return the value of the expression on the right side. When multiple "||" expressions are operated together, the value of the first expression is returned as true, if all expressions are evaluated as false, otherwise the value of the rightmost expression is returned.const aa = false || 'xx'; // aa输出为'xx'
"!!"
"!!"Forcibly convert the expression into a bool value, and the result of the operation is true or false.const aa = 'xx'; const bb = !!aa; // bb输出为true const cc = !!(NaN || undefined || null || 0 || '' ); // cc为false;
Implementing asynchronous update queue through nextTick() in Vuejs
How to implement 3D model in three.js Show
Angular CLI installation tutorial
How to implement Toast using ReactNative
How to do it using CommonsChunkPlugin Only by extracting public modules
There are complex operations about MVC in AngularJS
The above is the detailed content of How to convert bool value in js?. For more information, please follow other related articles on the PHP Chinese website!