
The Type Coercion Puzzle That Makes JavaScript Look Weird
Yesterday we looked at a small JavaScript puzzle involving the mysterious this keyword. JavaScript Interview Puzzle #2 we explored small small pieces of code that looked innocent but behaved in surprising ways. The goal of these puzzles is not to memorize tricks.The goal is to slowly understand how JavaScript actually thinks. Today’s puzzle is one of the most famous JavaScript interview questions. It looks so simple that many developers answer it confidently. And then… JavaScript does something unexpected. Let’s see if you can predict it. The Puzzle Look carefully at the following code. Before scrolling down, try to guess the output. console.log("5" + 3); console.log("5" - 3); console.log(true + true); console.log(false + 5); Take a moment. What do you think JavaScript will print? Many beginners assume the outputs should be something like this: 8 2 2 5 But that is only partially correct . The real output is: 53 2 2 5 Wait. Why did the first one become 53 instead of 8 ? Did JavaScript f
Continue reading on Dev.to JavaScript
Opens in a new tab



