Things you did not know about JavaScript

author-avatar
Paul Shan Wed Feb 19 2014
Sometimes because of smart development and sometimes because of its architectural design error, we often see some strange behaviors of JavaScript. A developer should be aware of these kind of things to develop a perfect program. Even these help you in interviews and JavaScript puzzle exams too. Today I've posted some of them in this article.

0.1 + 0.2 = 0.30000000000000004

Yes! In JavaScript if you add 0.1 and 0.2 it will not return 0.3! This is because 0.1 or 0.2 can’t be represented exactly as a floating point in JavaScript. It is a very close value; but not the exactly same value.  So minimal error + minimal error becomes visible error. ((0.1+0.2)==0.3) also returns false.

NaN is not equal to NaN

JavaScript has quite a few predefined values, like ‘NaN’ (not a number), undefined, null etc. For any of them, == operator returns true if the element in left and right hand side are same. Like (undefined==undefined) returns true; so does (null == null). But in case of NaN, things are different. (NaN==NaN) returns false.

Bitwise operators are slow

In most of the language bitwise operations are faster than the normal operations. But in case of JavaScript the bitwise operators convert the operand to 32 bit signed integer and turn the result back to 64 bit floating point. This makes the operation slower.

Global and window are confusing

All the JavaScript programs have a global object which contains all the variables, functions and other contexts. Even the browser variable ‘window’ is also a property of ‘global’, which is actually pointing to ‘global’. That means ‘window’ actually is ‘global.window’; and global.window = global. 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.toString());
if(a){
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

var obj1={a:1,b:2};
var obj2=obj1;
obj1.a=10;
console.log(obj2.a);
//It will print 10.

obj1=null;
console.log(obj2.a)

//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=1;
function myFunc(){
console.log(a);
console.log(a);
}
myFunc();

//Output is:
1
1

var a=1;
function myFunc(){
console.log(a);
var a=2;
console.log(a);
}
myFunc();

//Output is:
undefined
2
In the second case It’s printing undefined first, because JavaScript interpreter pushes the declaration at the beginning of the function. So, after interpretation the code actually becomes:
function myFunc(){
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
}
myFunc();

Few more

  1. typeof(object) = typeof(array) = typeof(null) = object
  2. 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.
  3. parseInt(“8A”) = 8. It executes upto the last numeric number.
Javascript

Written By

Paul Shan

Collections

  • E

    ES6

  • R

    React JS

  • C

    CSS

    Cascading style sheets only

  • S

    SEO

    Search engine optimization

  • E

    ES7

  • C

    CMS

    wordpress, drupal, jumla, magento and more


Show All

Tags

vue-js
advanced js
youtube
Web development
vue css
social share buttons
real-life-example
react datetime picker
progra
Online
MathJax
jsfiddle
jquery chart library
instant search using vue
handlebars
event loop
Design
console.log
best practices
imorph

Show All