Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Created July 30, 2018 22:49
Show Gist options
  • Select an option

  • Save gladchinda/fe2ef4f3b1ee52d91db36b60deaba2b4 to your computer and use it in GitHub Desktop.

Select an option

Save gladchinda/fe2ef4f3b1ee52d91db36b60deaba2b4 to your computer and use it in GitHub Desktop.
// 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