0.1 + 0.2 = 0.30000000000000004
((0.1+0.2)==0.3) also returns false
NaN is not equal to NaN
But in case of NaN, things are different. (NaN==NaN) returns false
Bitwise operators are slow
Global and window are confusing
This is little confusing, but this is how it is.
Suppose you have an object named myObject in your program’s global level. So if you directly write ‘myObject’, it will give you the value of ‘myObject’. But if you write ‘window.myObject’, it will first go to ‘global.window’, which is actually the ‘global’ itself, and then will find ‘myObject’. That means it executes an extra loop here.
Boolean is wired
var a=new Boolean() returns a truthy value, but its value is false. Again confusing, but this is how Boolean works.
Lets run an example.
var a=new Boolean();
console.log(‘a is truthy’);
You will find, the first console.log is printing false, but if(a) is true.
Actually ‘a’ is an object here. So when we wrote if(a), if executes it as if(object); hence returns true. But the default value of Boolean is false.
Behaviour of object and property
//It will print 10.
//will still print 10
Because obj2 was not pointing to obj1. The thing is both obj1 and obj2 was pointing to the same memory location. obj1=null, just made obj1 to point some other memory location.
Fun with ‘hoisting’
Another silly thing is JS is hoisting. For an example, let me write a piece of code in 2 different ways.
var a; // a is redeclared, but no value is assigned
console.log(a); // therefore it evaluates to undefined
a = 2; // now a = 2
console.log(a); // and then it logs to 2
- typeof(object) = typeof(array) = typeof(null) = object
- arr=[1,2,3,]; Now arr.length returns 3 in modern browsers. But IE 8 and below and some other browsers returns this as 4.
- parseInt(“8A”) = 8. It executes upto the last numeric number.