whatstheoutput_

#35: parseInt('0x1F') and parseInt(' 42abc')

Topic: numbers · Difficulty: ★★

console.log(parseInt('0x1F'));
console.log(parseInt('  42abc'));

Output

31
42

Why

parseInt is far more forgiving than Number() or unary +: it skips leading whitespace, and it stops parsing at the first character it can't use rather than failing the whole conversion. '0x1F' is auto-detected as a hexadecimal literal because of the '0x' prefix, so it parses 1F in base 16, which is 31. ' 42abc' skips the leading spaces, reads the valid digits '42', and then simply stops at the letter 'a', discarding the rest and returning 42 — no error, just a partial parse.

More numbers puzzles

Play today's puzzle →