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

How to convert bool value in js?

亚连
Release: 2018-06-19 14:04:25
Original
3779 people have browsed it

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

##Data typebool value conversionundefinedundefined is converted to falseObjectnull is converted to false, Others are trueBooleanfalse is converted to false, true is converted to trueNumber 0, NaN is converted to false, others are trueString"" 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;
Copy after login

The execution results of the above two sentences of code are the same. I have always used the above method when writing code before, but found that some single tests cannot be covered, resulting in a very low single test branch coverage. Change to the following This problem can be solved very well. The effect of these two sentences is the same.

"||"

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

"!!"

"!!"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;
Copy after login

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

Related articles:

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!

Related labels:
js
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