Puzzle 19Hexorcism
 const​ hex = ​"0x1E"​;
 
 console.log(hex - 0);

Guess the Output

images/aside-icons/important.png

Try to guess what the output is before moving to the next page.

images/hline.png

You might have anticipated that the output would be NaN since “0x1E” is a string, but this code will actually output:

 30
images/hline.png

Discussion

This JavaScript code outputs 30 because of the way JavaScript handles arithmetic operations and type conversions. The "0x" part is a prefix used ...

Get JavaScript Brain Teasers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.