When I graduated from college and applied for a job, I used to do some bizarre interview questions. Here’s a trick question. It’s not really an interview question. You can use it to trick your programmer friends.

How to implement 2+2=5 in JavaScript?

Here are the answers.

Create a TXT file, change the suffix to.html, and open it in your browser.

< HTML > <script> g = function () {H = 3 return H + H} f = function () {degree = 2 return degree + H} // 3 + 3 = 6 alert(g()) //  2 + 2 = 5 alert(f()) </script> </html>Copy the code

Two dialog boxes will pop up successively, showing 6 and 5.

However, in the second function f, it is clear that H is assigned 2. 2 + 2 should be equal to 4. Why is the answer 5????

Let’s use Chrome developer Tools to debug it and see what’s going on.

Why is it that f has two H’s that look exactly the same, but one has the value of 2 and one has the value of 3?

By now, the naked eye can’t help us. Here comes winHex, a hexadecimal file editing and disk editing magic.

A search for ASCII on Baidu shows that the hexadecimal code of uppercase H is 48.

Then we open the code file with WinHex, and sure enough we find the hexadecimal code of 48 with a capital H.

Similarly, the hexadecimal code for the plus sign “+” is 2B:

The space is 20.

So here’s the answer: the variable “H” in front of the plus sign that looks like a capital” H” to the naked eye isn’t actually AN “H.” It’s coded in hexadecimal CE 97. Its value is given 2 in f, and capital H is given 3, 2 + 3 = 5, so what we see in the popover is 5.

This question is not about closures in JavaScript.

For more of Jerry’s original technical articles, please follow the public account “Wang Zixi” or scan the following QR code: