Created
July 30, 2018 22:49
-
-
Save gladchinda/fe2ef4f3b1ee52d91db36b60deaba2b4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // METHOD 1: Short-circuiting | |
| // Using the logical OR (||) operator | |
| function convertToBase(number, base) { | |
| number = parseInt(number) || 0; | |
| base = parseInt(base) || 10; | |
| return number.toString(base); | |
| } | |
| // METHOD 2: Ternary (?:) operator | |
| // With additional type check (safer option) | |
| function convertToBase(number, base) { | |
| number = (typeof number !== "undefined") ? parseInt(number) : 0; | |
| base = (typeof base !== "undefined") ? parseInt(base) : 10; | |
| return number.toString(base); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment