Home  >  Article  >  Web Front-end  >  Summary of using && and || in Javascript_javascript tips

Summary of using && and || in Javascript_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:28:411096browse

&& and || in ordinary cases are relatively simple and will not be discussed here.

Prepare two objects for the following discussion.

Copy code The code is as follows:

var alice = {
name: "alice" ,
toString: function () {
return this.name;
}
}

var smith = {
name: "smith",
toString: function () {
return this.name;
}
}

In javascript, && can not only be used for boolean type, nor can it only return Boolean type results.
l If the first operand is of type Boolean and the value is false, then return false directly.
l If the first operand is of type Boolean and the value is true, and the other operand is of type object, then this object will be returned.
l If both operands are of object type, return the second object.
l If any operand is null, then null is returned.
l If either operand is NaN, return NaN.
l If any operand is undefined, return undefined.


alert(false && alice); // false
alert(true && alice); // alice

alert(alice && smith); // smith
alert(smith && alice); // alice

alert(null && alice); // null
alert(NaN && alice); // NaN
alert(undefined && alice); // undefined
alert(alice && undefined); // undefined

For ||, it is not only used for Boolean type, nor only returns Boolean type results.
In fact, null, undefined, and NaN will be treated as false. And the object is treated as true.

l If the first operand is of type boolean and the value is true, then return true directly.
l If the first operand is of type Boolean and the value is false and the second operand is object, then the object object is returned.
l If both operands are of type object, return the first object.
l If both operands are null, then null is returned.
l If both operands are NaN, return NaN.
l If both operands are undefined, then undefined is returned.
alert(false || alice); // alice

alert(true || alice); // true

alert(alice || smith); // alice

alert(smith || alice); // smith

alert(null || alice); // alice

alert(alice || null); // alice

alert(null || null); // null

alert(NaN || alice); // alice

alert(alice || NaN); // alice

alert(NaN || NaN);                               // NaN

alert(undefined || alice); // alice

alert(alice || undefined); // alice

alert(undefined || undefined); // undefined

You don’t need to make it so complicated. I recommend you read this part of the explanation.
a && b: Convert a and b to Boolean types, and then perform logical AND. True returns b, false returns a
a || b: Convert a, b to Boolean type, and then perform logical OR, true returns a, false returns b
Conversion rules:
Object is true
Non-zero number is true
Non-empty strings are true
Others are false

Related articles can refer to the following articles to summarize
js AND or operator || && Magical Uses
JS uses AND or operator priority to implement if else conditional judgment expression
Alternative usage skills of javascript && and || algorithms

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