Skip to content

Instantly share code, notes, and snippets.

@katepapineni
Created July 30, 2020 02:53
Show Gist options
  • Save katepapineni/973ed0d33338baace82369e631ca3bbf to your computer and use it in GitHub Desktop.
Save katepapineni/973ed0d33338baace82369e631ca3bbf to your computer and use it in GitHub Desktop.
/* Ternary vs. If-Else */
const min = 2;
const max = 7;
// As Ternary operation
const getNumTernary = (num) => {
return num < min ? min : num > max ? max : num;
}
// As If - Else statements
const getNumIfElse = (num) => {
if (num < min) {
return min;
} else if (num > max) {
return max;
} else {
return num;
}
}
console.log(getNumTernary(5)); // min < 5 < max so returns 5
console.log(getNumIfElse(5)); // min < 5 < max so returns 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment