Skip to main content

Unknown Facts about JavaScript

Along with HTML, CSS; JavaSript is one of the three main things of the www. Today I will be discussing a few facts, which might help you respect JavaScript even more. Even for developers that interact with it daily, some part of the language remains unexplored. I myself was surprised as I had no idea about maximum of the facts I am going to discuss now.

1. Presumably, JavaScript has 2 sets of Zeros.: -0 and +0. Although both of them are considered to be equal. This so happens because both (-0).toString() and (+0).toString() results to 0. And hence the console shows both -0 and +0 as simply 0.

1
2
3
4
+0
→ 0
-0
→ 0


2. The XOR(^) is used in cryptography.
1
2
3
4
5
6
7
8
9
10
11
// Alice and Bob share the same secret key:
let key = 123;
// Alice wants to send Bob a number
let msg = 42;
// But before sending it, Alice encrypts it:
msg = msg ^ key // or directly: msg ^= key
→ 81
// Bob receives 45, but knowing the key is 123 he knows to decrypt it:
81 ^ key
→ 42
// Now Bob can enjoy the message from Alice
3. NaN(Not a Number) is itself a special number. A number which does not equals itself. Nan is neither finite nor infinite, neither positive nor negative.

1
2
3
4
5
6
7
8
9
10
11
12
// Check if it's finite
isFinite(NaN)
false
// Comparing with infinity, it will always give us false
Infinity > NaN
false
> Infinity < NaN
false
-Infinity < NaN
false
> -Infinity > NaN
false
4. We can easily find whether a particular element is a part of an array or not by using the NOT operator(~).

5. Floating numbers are dangerous in JS. for eg: 0.1 + 0.2 won't result in 0.3 but 0.1 and 0.3 will surely result in 0.4.
Here is why:
0.1+0.2  //0.30000000000000004
0.1+0.3  //0.4
0.2+0.4  //0.6000000000000001
0.2+0.5  //0.7
0.3+0.6  //0.8999999999999999
0.3+0.7 //1 


Comments