Home  >  Article  >  Web Front-end  >  || and && usage in JavaScript (detailed explanation)

|| and && usage in JavaScript (detailed explanation)

青灯夜游
青灯夜游forward
2018-10-08 14:49:502157browse

&& and || are particularly widely used in JQuery source code. I found some examples on the Internet as a reference and studied their usage:

##&&

function a(){
    alert("a");
    return true;
}
function b(){
    alert("b");
    return true;
}
var c=a()&&b();
alert(c);

a() && b(): If true is returned after executing a(), b() is executed and the value of b is returned; if false is returned after a() is executed, the entire expression is returned The value of a(), b() is not executed;


||

function a(){
    alert("a");
    return true;
}
function b(){
    alert("b");
    return false;
}
var c=a()||b();
alert(c);

a() || b(): if executed If true is returned after a(), the entire expression returns the value of a(), and b() is not executed; if false is returned after executing a(), b() is executed and the value of b() is returned;

&& has a higher priority than ||

alert((1 && 3 || 0) && 4); //Result 4 ①

alert(1 && 3 || 0 && 4); //Result 3 ②
alert(0 && 3 || 1 && 4); //Result 4 ③

Analysis:

Statement ①: 1&&3 Return 3 => 3 || 0 Returns 3 => 3&&4 Returns 4
Statement ②: Execute 1&&3 first and return 3, then execute 0&&4 and return 0, and finally compare the execution results with 3||0 Return 3
Statement ③: First Executing 0&&3 returns 0, executing 1&&4 returns 4, and the final execution result is compared with 0||4 and returns 4

Note: All non-0 integers are true, and undefined, null and empty string "" are false.

In javascript, && can not only be used for boolean types, 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 type object, then the second object is returned.
l If any operand is null, then null is returned.
l If any operand is NaN, return NaN.
l If any operand is undefined, then undefined is returned.

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. result.

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 true is returned 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, then the first object is returned.
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

No need to make it so complicated. I recommend you read this part of the explanation

a && b: Convert a and b to Boolean type, then perform logical AND, true returns b, false returns a
a || b: Convert a, b to Boolean type, then perform logical OR, true returns a, false returns b

Conversion rules:
Object is true
Non-zero number is true
Non-empty string is true
Others are false

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

The above is the detailed content of || and && usage in JavaScript (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete