Last active
June 1, 2020 11:13
-
-
Save luc4leone/cafa25479d69c0d43e88b0f4cdc22a0a to your computer and use it in GitHub Desktop.
Useful way to read JavaScript AND and OR expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I find it useful to read expr1 && expr2 as "if expr1 is true, than expr2" | |
// By definition, expr1 && expr2 returns expr2 if expr1 can be converted to true; otherwise, it returns expr2. | |
// Seeing things through an if statement helps me build this sort of mental shortcut. | |
function and(expr1, expr2) { | |
if (Boolean(expr1) === true) { | |
return expr2; | |
} | |
return expr1; | |
} | |
// I find it useful to read expr1 || expr2 as "if expr1 is true, then expr1" | |
// By definition, expr1 || expr2 returns expr1 if expr1 can be converted to true; otherwise, it returns expr2. | |
function or(expr1, expr2) { | |
if (Boolean(expr1) === true) { | |
return expr1; | |
} | |
return expr2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment