Last active
February 5, 2025 02:08
-
-
Save Raiondesu/2bfafd12ec7fdb6f3168d4062d430c16 to your computer and use it in GitHub Desktop.
Ways to check for a condition in javascript
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
// (Very different) ways to write an "if" in JavaScript | |
// 1 | |
if (condition) { | |
expression; | |
} | |
// 2 | |
condition ? expression : {}; | |
// 3 | |
condition && expression; | |
// 4 | |
switch (true) { | |
case condition: | |
expression; | |
break; | |
} | |
// 5 | |
while (condition) { | |
expression; | |
break; | |
} | |
// 6 | |
for (;condition;) { | |
expression; | |
break; | |
} | |
// 7 | |
[() => expression] | |
.find(() => condition)(); | |
// 8 | |
[0][+condition] ??= expression; | |
// 9 | |
[!condition][+[]] ||= expression; | |
// 10 | |
({[!0]: () => expression})[condition]?.() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment