Skip to content

Instantly share code, notes, and snippets.

@y13i
Last active February 26, 2017 19:17
Show Gist options
  • Save y13i/33784c22441124e73586d3a8c9396c35 to your computer and use it in GitHub Desktop.
Save y13i/33784c22441124e73586d3a8c9396c35 to your computer and use it in GitHub Desktop.
if statement vs ternary operator
const num = Math.floor(Math.random() * 10);
let evenOrOdd: string;
if (num % 2 === 0) {
evenOrOdd = 'even';
} else {
evenOrOdd = 'odd';
}
console.log(`${num} is an ${evenOrOdd} number.`);
const num = Math.floor(Math.random() * 10);
const evenOrOdd = (num % 2 === 0) ? 'even' : 'odd';
console.log(`${num} is an ${evenOrOdd} number.`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment