Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert Bool value in js

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to convert Bools in js. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Preface

The first thing to know is that six values in js are false, which are 0,', null, undefined, NaN and false, and the others (including {}, [], Infinity) are true.

The Boolean value of an object can be obtained by using the Boolean () function or twice, such as Boolean (undefined) and!! undefined can also get a Boolean false

Boolean values for 0,', null, undefined, NaN, {}, [], Infinity are false false false false false true true true.

So one thing we know is that the Boolean value of the object is true, even if it is the object {}.

Bool value conversion

Data type Boolean conversion undefinedundefined to falseObjectnull to false, others to trueBooleanfalse to false,true to trueNumber0,NaN to false, others to trueString "" to false, others to true

"&"

The operation algorithm of the "&" operator in javascript is as follows:

If the value of the left expression is true, the value of the right expression is returned; otherwise, the value of the left expression is returned. When multiple & & expressions are evaluated together, the value of the first expression as false is returned, and if all expressions are true, the value of the rightmost expression operation is returned.

Const aa = {'name':' xx'}; const bb = aa & & aa.age; / / bb output as undefined;let cc;const dd = cc & & cc.name? Cc.name: undefined; / / dd output as undefinedconst dd = cc & & cc.name; / / dd output as undefined

The execution result of the above two sentences of code is the same. The above method has been used to write the code before, but it is found that some single tests can not be covered, resulting in a very low branch coverage of the single test. Changing the following way can solve this problem very well. The effect of these two sentences is the same.

"| |"

The "| |" operator in javascript operates as follows:

If the value of the left expression is true, the value of the left expression is returned; otherwise, the value of the right expression is returned. When multiple "| |" expressions are evaluated 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 output is' xx'

"!!"

"!!" Performs an operation that forcibly converts an expression to a Bool value, resulting in either true or false.

Const aa = 'xx';const bb =!! aa; / / bb output is trueconst cc =! (NaN | | undefined | | null | | 0 | |'); / / cc thanks for reading for false;! This is the end of this article on "how to convert Bools in js". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report