Skip to content

Instantly share code, notes, and snippets.

@luc4leone
Last active June 1, 2020 11:13
Show Gist options
  • Save luc4leone/cafa25479d69c0d43e88b0f4cdc22a0a to your computer and use it in GitHub Desktop.
Save luc4leone/cafa25479d69c0d43e88b0f4cdc22a0a to your computer and use it in GitHub Desktop.
Useful way to read JavaScript AND and OR expressions
// 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