Funny Javascript & 7 Little Questions

Surender Neelakantan
2 min readJun 9, 2020

Javascript is somewhat funny. I was surprised when I came across some of the weird concepts in Javascript. There are many such concepts in Javascript. Here, I would like to highlight these 7 concepts.

If you are looking for something interactive, you can view this video where all these 7 concepts are explained clearly using examples. Don’t miss that fun.

Summary of the 7 funny concepts:

1. Floating point addition

We all know 0.1 + 0.2 is equal to 0.3. But unfortunately, someone must explain this to Javascript. According to Javascript, 0.1 + 0.2 is not equal to 0.3. If you doubt that, you can watch the video or write a program to test that.

2. Long number addition

Try to add 1 to 10000000000000000. You will be surprised to see the output. And then, instead of 1, try adding 1.1 to 10000000000000000. Again, you will be surprised.

3. Array addition

I have an array with three numbers [1, 2, 3] which I tried to add with another array of three numbers [4, 5, 6].

Logically, I should get [1, 2, 3, 4, 5, 6] or [5, 7, 9]. But unfortunately, I got [1, 2, 34, 5, 6].

Array of 3 numbers + Array of 3 numbers = Array of 5 numbers

4. Addition, Subtraction, Confusion

‘5’ — 3 = 2. Javascript is very intelligent and it could identify the number in character to the original number and do the calculation.

‘5’ + 3 = 53. But unfortunately, Javascript was absent when addition was taught in the class. It just concatenates.

5. Boolean addition

When you try to add true and true, you get 2. Yes, true + true is 2. Javascript converts true to 1 and false to 0.

Could you please get me TRUE cup of coffee with FALSE added sugar? Sounds funny.

6. Max — Min logic

According to Javascript, Math.max() is lesser than Math.min(). You can verify that.

7. Restructuring program

Some people use opening brace in the same line.

for(let i = 0; i < 100; i++) {

}

Some people use opening brace in the next line.

for(let i = 0; i < 100; i++)

{

}

But, you cannot do this for all statements in Javascript. An example is there in the video.

Happy coding with Javascript!

--

--