Things you did not know about JavaScript

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.

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

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.

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:

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.
About This Author

Hello! I am Paul Shan, a JavaScript Expert, Full Stack and DevOps Engineer cum Consultant based out of Bengaluru, India.

  • 0.1 or 0.2 can’t be represented exactly as a floating point in JavaScript. Wrong, 0.1 and 0.2 can be represented as floating point numbers, its their addition which can’t be represented.

    • metalshan

      Whether or not a rational number has a terminating expansion depends on the base. For example, in base-10 the number 1/2 has a terminating expansion (0.5) while the number 1/3 does not (0.333…). In base-2 only rationals with denominators that are powers of 2 (such as 1/2 or 3/16) are terminating. Any rational with a denominator that has a prime factor other than 2 will have an infinite binary expansion. This means that numbers which appear to be short and exact when written in decimal format may need to be approximated when converted to binary floating-point. For example, the decimal number 0.1 is not representable in binary floating-point of any finite precision; the exact binary representation would have a “1100” sequence continuing endlessly:

      e = −4; s = 1100110011001100110011001100110011…,

      where, as previously, s is the significand and e is the exponent.

      When rounded to 24 bits this becomes

      e = −4; s = 110011001100110011001101,

      which is actually 0.100000001490116119384765625 in decimal.

  • typeof(object) = typeof(array) = typeof(null) = “object”
    its not object, its “object”

    • metalshan

      typeof always returns the type as a string. I think this much of common sense normal people have to figure it out that I wanna say it a object type. Every time I need not to put everything inside double quotes to tell them it’s a string

  • Guest

    I checked in Firefox and Chrome but I couldn’t find global object. Is it implicit?

    • metalshan

      Global is not accessible by the keyword global. whenever you write a variable name, the javascript compiler finds it in ‘global.thatObjectName’

  • I checked Firefox and Chrome but could not find global object.

    • metalshan

      Global is not accessible by the keyword global. whenever you write a variable name, the javascript compiler finds it in ‘global.thatObjectName’

  • Can Love

    O , today i learned a lot , he he~~

  • Ashutosh