Importance of double ampersand: logical AND(&&)in JavaScript

Saves time and code length.
Gets rid of else statement.
Basic Example:
> if/else statement
let x= 2, y =5;
if(y>x){
console.log(“y is greater than x”);
return;
}

Using logical AND (&&) as short-circuit property
let x= 2, y =5;
y>x …


This content originally appeared on DEV Community and was authored by Mohit Saud

  • Saves time and code length.
  • Gets rid of else statement. Basic Example: > if/else statement let x= 2, y =5; if(y>x){ console.log("y is greater than x"); return; }

Using logical AND (&&) as short-circuit property
let x= 2, y =5;
y>x && console.log("y is greater than x");

  • The difference the Short-Circuit property makes that you are able to write the code in few lines and is considered as one of the important features of JavaScript and hence making the JavaScript so powerful.


This content originally appeared on DEV Community and was authored by Mohit Saud


Print Share Comment Cite Upload Translate Updates
APA

Mohit Saud | Sciencx (2021-11-09T04:36:20+00:00) Importance of double ampersand: logical AND(&&)in JavaScript. Retrieved from https://www.scien.cx/2021/11/09/importance-of-double-ampersand-logical-andin-javascript/

MLA
" » Importance of double ampersand: logical AND(&&)in JavaScript." Mohit Saud | Sciencx - Tuesday November 9, 2021, https://www.scien.cx/2021/11/09/importance-of-double-ampersand-logical-andin-javascript/
HARVARD
Mohit Saud | Sciencx Tuesday November 9, 2021 » Importance of double ampersand: logical AND(&&)in JavaScript., viewed ,<https://www.scien.cx/2021/11/09/importance-of-double-ampersand-logical-andin-javascript/>
VANCOUVER
Mohit Saud | Sciencx - » Importance of double ampersand: logical AND(&&)in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/09/importance-of-double-ampersand-logical-andin-javascript/
CHICAGO
" » Importance of double ampersand: logical AND(&&)in JavaScript." Mohit Saud | Sciencx - Accessed . https://www.scien.cx/2021/11/09/importance-of-double-ampersand-logical-andin-javascript/
IEEE
" » Importance of double ampersand: logical AND(&&)in JavaScript." Mohit Saud | Sciencx [Online]. Available: https://www.scien.cx/2021/11/09/importance-of-double-ampersand-logical-andin-javascript/. [Accessed: ]
rf:citation
» Importance of double ampersand: logical AND(&&)in JavaScript | Mohit Saud | Sciencx | https://www.scien.cx/2021/11/09/importance-of-double-ampersand-logical-andin-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.