Datatypes in javascript

If you cannot answer the following questions, this post is for you.

How do I create an integer in javascript?
Will ‘a’ === “a” be true or false?
Why typeof function is called without parenthesis?
Why typeof null returns “object”?
What all possible values does typeof returns?

Datatypes are the basic building blocks of any language. When I started learning java, one of the first things was to learn difference between int, float and double. In fact converting a float to int was an important step in separating the digits of any number. And that’s how you start writing your code. Javascript is not a typed language which means you dont decide the type of data for any variable. You just declare a variable which can store any type of value.

var someVariable;
someVariable=100;
someVariable="Hundred"
someVariable=true
someVariable={"someKey" : "someValue"}
someVariable=function getSquare(x){ return x*x}

All above statements will work and if executed in sequence will assign someVariable a function as the last assignment would do. Because of this we don’t need do know data type of variables in advance and thereby we don’t need to tell JS in advance too.

Formally we have below datatypes available in JS according to the ECMA standard

Number
String
Boolean
Object
null
undefined
Symbol

To get the datatype of any value we use typeof operator. Though it sounds like a function but typeof is an operator which accepts a single operand. The typeof operator returns following values

“number”
“string”
“boolean”
“object”
“undefined”
“function”

If you see in the above list there is one thing which typeof operator returns but is not there in the data type list which is “function”. Why is this so? The typeof operator is a special operator that returns one of the specific set of values and that set of values is not exactly the set of all data types that JS supports.

Since there are no fixed types, the typeof operator comes to rescue. It helps you to determine the type at runtime. And being a functional language, it would help a lot to identify if the variable is a function or data. Using ‘typeof’ to determine whether the value is a function or data is a popular way and used extensively in application code as well as libraries.

Coming to the “object” datatype. Any object other than function will return “object” when typeof operator is used. It doesn’t matter what kind of object it is.

typeof {} // "object" - literal objects
typeof [] // "object" - Arrays are just plain objects with some special capabilities such as length property
typeof Object.prototype // "object" - Prototype objects are also objects

In javascript all numbers are just numbers. So var num = 4 and var num = 4.0 are exactly same and will result in same end result.
In javasctipt all strings are just strings whether its a single character or multiple. So var str=”s” is exactly same as var str=’s’.
In javascript there are two boolean values true and false. Do note that these values are different from “true” and “false“.

Now the infamous typeof null evaluation. If you try this you will see “object” as output while typeof undefined would return “undefined” and not “object”. Any ideas why so?

null in the memory is stored as 0x00000000 which means a null value. And objects are also implemented in such a way that their hex code starts with 0x… The typeof operator checks the first two codes which in case of null is ‘0x’ and thus returns “object”. This is mostly seen as a bug in the implementation but cannot be corrected now as it would result in a number of websites going into error.

Javascript does not have a way to directly get the type of the objects created out of function. For e.g.

var Shape = function(id){this.id = id;};

var s1 = new Shape(1);

typeof s1 // "object"

If you want to know the type of the the object s1, you have a few ways to identify but not concrete.

s1.constructor.name // Shape

Another way to identify the type is to use the native toString method but feels a kind of hack

Object.prototype.toString.apply("str") // [object String]

You must have got some idea about how data types work in javascript.

Leave a Reply

Your email address will not be published. Required fields are marked *