$ ./guess_the_languageProblem Solving — predict the output
Problem Solving — predict the output · easy · #223

What is the output?

function twice(x: number | string) {
  return typeof x === 'number' ? x * 2 : x.repeat(2);
}
console.log(twice(3), twice('ab'));
  1. 6 NaN
  2. 6 abab
  3. 33 abab
  4. 6 ab2
The typeof check narrows x: numbers are doubled, strings are repeated.
Play Problem Solving ▸