Home > Web Front-end > JS Tutorial > body text

Alternative usage techniques of javascript && and || algorithms_javascript techniques

WBOY
Release: 2016-05-16 18:41:00
Original
969 people have browsed it

&& and || are particularly widely used in the JQuery source code. Since I have not systematically learned js, I can only roughly understand it myself. I hope you can give me some guidance.
A rough understanding is as follows:

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(), Then the entire expression returns the value of a(), and b() is not executed;
a() || b(): If true is returned after executing a(), the entire expression returns the value of a(), 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 ||

as follows:
 
Code

Copy code 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 ③

Analysis:
Statement ①: 1&&3 returns 3 => 3 || 0 returns 3 => 3&&4 returns 4
Statement ②: Execute 1&&3 first and return 3, then execute 0&&4 and return 0, the final execution result is compared with 3||0 and returns 3
Statement ③: execute 0&&3 first and return 0, then execute 1&&4 and return 4, the final execution result is compared with 0||4 and return 4

Note: non-0 The integers are all true, undefined, null and the empty string "" are false.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!