Home  >  Article  >  Web Front-end  >  js logical operators ||_javascript skills

js logical operators ||_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:26:011122browse

So check the introduction of js:

1. Logical OR operator ||:

When both operands of operator || are Boolean values, it will Performs a Boolean OR operation.

It first calculates the first operand, and if the value of this expression can be converted to true, then it returns the value of the expression on the left. Otherwise, calculate the second operand

Even if the operand of the || operator is not a Boolean value, it can still be regarded as a Boolean OR operation, because no matter what type the value it returns is, it can be converted is a Boolean value.

On the other hand, || is used for non-Boolean operands, which takes advantage of its feature of returning non-Boolean values. This use of this operator usually selects the first defined and non-null value in a set of alternative values ​​(that is, the first value that will not be converted to false)

Example :

var max=max_width || preferences.max_width || 500

Statement:
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