Skip to content

Instantly share code, notes, and snippets.

@Raiondesu
Last active February 5, 2025 02:08
Show Gist options
  • Save Raiondesu/2bfafd12ec7fdb6f3168d4062d430c16 to your computer and use it in GitHub Desktop.
Save Raiondesu/2bfafd12ec7fdb6f3168d4062d430c16 to your computer and use it in GitHub Desktop.
Ways to check for a condition in javascript
// (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