JavaScript data type

Data Type

In Programming language a data type determines what kind of data that variable holding.

e.g. In C programming language int count; would mean that the count variable only can hold integer data type. If someone try count = “hello” a compiler error will be thrown. This kind of data type is called static data type.

However in javascript data type is dynamic, so one need not to specify any data type in front of the variable, it only can be declared by the keyword var . So var count; in javascript would create a variable of named count. Consequent assignment of different data type count = 1; count = “hello”; count = true; will not create any run time exception.

In otherway to say data type of a variable javascript engine determines at the run time.

Cinque Terre

Primitive DataType

Primitive are elemental single value data type. Elemental means they have unique characteristic and they serve unique purpose.

  1. undefined : A key word variable which javascript engine assigns when variable are created without any defined value. Do not use this keyword to assign value.

  2. null : A specific memory space which can be used to point any variable. You can assign null to a variable. It may look similar with undefined but remember null can be used by you as per situation and can be used for comparison e.g. count === null .

  3. string : It holds charter data set enclosed within “” or '' .

  4. boolean : it holds true/false .

  5. number : In javascript only one floating point number that is 'number'.


In the below diagram you can see we are adding two different type of variable, one is number second one is string and javascript has to take decision to come up with the result.

Cinque Terre

Here is the dilemma as javascript a dynamic type language so at runtime this kind of scenario can appear, and javascript engine need to take decision based on that.

In this case even though 2 is number it treats like a string and it gets added with 3 to produce string concatenation result 23.

This type of forcing data type from one to another type based on situation is named as coercing.

This is very important to realize that unlike other programming language which generally throws run time exception javascript would carry on producing sometime unintended result.

Back Next