Created
July 30, 2020 02:53
-
-
Save katepapineni/973ed0d33338baace82369e631ca3bbf to your computer and use it in GitHub Desktop.
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
/* 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