These two operators are often used, and they are always confused and recorded. . .
a() && b() : If true is returned after executing a(), then b() is executed and the value of b is returned; if false is returned after executing a(), the entire expression returns the value of a(), and b() does not execute;
a() || b() : If true is returned after executing a(), the entire expression returns the value of a(), and b() does not execute; if false is returned after executing a(), b() is executed and the value of b() is returned;
&& Priority is higher than ||
The code is as follows
alert((1 && 3 || 0) && 4); //Result 4 ①
alert(1 && 3 || 0 && 4); //Result 3 ②
alert(0 && 3 || 1 && 4); //Result 4 ③
analyze
Statement ①: 1&&3 Return 3 => 3 || 0 Return 3 => 3&&4 Return 4
Statement ②: First execute 1&&3 and return 3, then execute 0&&4 and return 0, and finally execute results comparison 3||0 and return 3
Statement ③: First execute 0&&3 and return 0, then execute 1&&4 and return 4, and finally execute results comparison 0||4 and return 4
Note: Integers that are not zero are true, undefined, null and empty strings are false.