1024programmer JavaScript How to use boolean operators in js

How to use boolean operators in js

Operation rules: If the first operator returns true, return the value of the second operator (not a Boolean value); if the first If an operator returns false, the first operator is returned, and the second operator is no longer evaluated (short circuit)

// &#  39; a' converted to boolean is true, so directly return the second operator ''
 & # 39; a & # 39; & & & # 39; & # 39;
 // & # 39; & # 39; converted to boolean is false, so return the first operator directly & # 39; & # 39;
 '' && 'a'

Or operator (||)

Or Operators are also used for the value of multiple expressions

Operation rules: If the Boolean value of the first operator is true, then return the value of the first operator directly; if the first operator If the Boolean value of an operator is false, then return the value of the second operator

// Boolean value of ''  is false, so here returns 'b'
 & # 39; & # 39; || & # 39; b & # 39;
 // The boolean value of 'b' is true, so here returns 'b'
 & # 39; b & # 39; || & # 39; & # 39; 

The or operator commonly used to set a default value for a variable

Ternary operator

expression1 ? expression2 : expression3;

If the Boolean value of expression1 is true, return expression2; If the Boolean value of expression1 is false, return expression3.

Recommended tutorial: js introductory tutorial

The above is the detailed content of how to use Boolean operators in js, please pay attention to other related articles on 1024programmer.com for more!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/how-to-use-boolean-operators-in-js/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索