The application example of JS short circuit principle

Source: Internet
Author: User

  As the title says, JS | | And && features help us streamline code while also bringing down code readability. This is a good example of what we need to weigh on ourselves.

In the JS logical operation, 0, "", null, False, undefined, Nan will all be sentenced to false, others are true.    | | Calculates the first operand and, if it can be converted to true, returns the value of the expression on the left, otherwise the second operand is computed. Even if | | The operator's operand is not a Boolean, and it can still be treated as a Boolean or operation, because it can be converted to a Boolean value regardless of the type of value it returns.   Take advantage of the attributes it returns for a non-Boolean value: Use for non-boolean operands to select the first defined and Non-null value in a set of alternative values (the first value not false)     Example:  var max = Max_ width | | Obj.max_width | | ;  var attr = attr | | ""; This operation is often used to determine whether a variable is defined, and if it is not defined, give him an initial value, which is useful when defining a default value for a function's arguments.     &&amp, which evaluates the first expression and, if it is false, does not handle the second expression, otherwise the subsequent expression continues to be processed. Selects from left to right the value of the first expression that is not true, and returns the value of the last expression if it has not been found.     Example: (the flavor also needs to be carefully pondered)     2 && ' s1 ' && ' 123 ' && ' SSS ' the value of the expression equals ' sss '   2 The value of the;& ' s1 ' && ' && ' sss ' expression equals '   2 && ' s1 ' && NaN && ' SSS ' the value of the expression equals NAN&N Bsp   if (a >=5) {  alert ("Hello"); }  can be simplified to:  a >= 5 && alert ("Hello");    typeof 5 and typeof!! 5 of the difference, this is a more rigorous way of writing,!! The role is to convert a variable of another type into a bool type. For example, if (!! attr) => if (attr)     JS | | and &&ampFeatures help us streamline the code while also bringing down the readability of the code. It's up to us to weigh it.     JS in the smart implementation of the Startwith function, alert (!) Asdf '. IndexOf (' s ') = "!0 = True  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.