top of page

Data Types


We have all heard of this term somewhere or the other but what actually are Data types and why do we even need them in the first place?

A Data type or simply type is an attribute(property) of data that tells the compiler or interpreter how the programmers like us intend to use the data.

Basically, they convey what we want to the compiler or interpreter.

So, As we have a clear understanding of what data types actually are, let us look at Data Types in Javascript.

In JavaScript data types are divided into two major categories -

  1. Primitive Data Type

  2. Non-Primitive(Reference) Data Type.

Both these categories are further divided into various categories and we will have a detailed study of each and every one of them.

Var, const and let

Whenever we need to store something around us what do we need?

We need a box or a container to hold that thing for us.

Similarly to store a value in javascript, we need a container for it.

There are three ways to store values in javascript and that are

  • Variable

  • Constants

  • Let

All three of them can contain any type of data whether it is string, numbers or boolean. Whatever we decide to store in them their type will be set accordingly


Variables are the data containers declared using the var keyword. We decide the name for a variable and assign a value to it.

The process of creating a variable using var keyword is known as declaration and the process of assigning the variable a value is known as initialisation.

Now there is one more interesting feature to variables, which we derive from its name itself, that is its value can vary, that is variables can be redeclared and updated.

In the example above the variable first is declared and initialised with the value “hello” and later it is redeclared and assigned a value “world”


As the name suggests constants are the data containers in which the value remains the same that is constant throughout the program.

We can declare a constant using const keyword. After assigning a value to a constant we cannot redeclare or update it.

Here the constant is named as first and contains a string value that is “hello”.


Let data containers are very similar to var but the only difference is let can be updated but not redeclared.It allows us to update the the value assigned to it but we cannot redeclare it.

Notice the difference between var and let. Here we just updated the value of first instead of redeclaring it again.

Characteristics of Javascript

But before understanding data type in detail, let us understand some major characteristics of Javascript.

  • Javascript is a dynamic type language i.e we don’t need to specify the type of data that we will store in a variable or a constant because Javascript Engine takes care of it.

Okay so what exactly does the above statement implies?

It means that we can simply assign any value to a variable or a constant like string, number, or boolean.

We can consider variables or constants as a box in which we can store any type of data.

Let us have a look at how we will implement this in code.

In the example above temp is a variable and is holding a number and temp1 is also a variable but is holding a string. The point to notice is that we did not specify the type for any one of them. Javascript automatically identifies our choice and act accordingly.

  • Javascript variables are loosely typed which means that you can assign any type of literal values to a variable e.g. string, integer, float, boolean, etc, and redeclare it later. That means we have the flexibility to change our minds anytime during the program and JavaScript will act accordingly.

Now let us look at our good friend box. Here it first contained a string value which was then replaced by a number and finally was replaced by a boolean value.

Let us have a look at some code.

In the example above we first declared the variable first as a number and then as a string.

Primitive Data Types


As we know that we need to tell the interpreter about our choice of data types. But think of a situation in which each programmer is creating their own data types according to their preference. What will happen? Our interpreter will be super confused about what to do and how to do it. Moreover no programmer will be able to understand the code of other programmers. Ohh!!! What confusion..

But thank god there is an easier way. Javascript defines the 5 most data types for us to choose from just like a restaurant offers us a menu.

These data types act as building blocks and help to ensure clear and smooth communication between the programmer and the interpreter. These are 5 types of primitive data types in JavaScript-

Now let us look at each and every data type in detail-

  1. Strings

Strings are a series of characters like “Learning JavaScript”. Whenever we want to store a textual value we use strings.

We can write strings using-

  1. Single quotes

  2. Double quotes

  3. Backticks

Let us look at an example of how to write strings in JavaScript.

In the example above we stored strings using single quotes, double quotes, and backticks where single quotes and double quotes are practically the same but backticks provide us with some functionality. When we want to include the value of a Javascript variable inside an expression we can do that in the following way-

The output for the above example will be Hello user 1. We see that the value of num is replaced by its original value, this is exactly how backticks help us.

Primitive values like Strings cannot have properties or methods(because they are not objects) but with Javascript, methods, and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.

That means we can play around with strings a lot!!! So let us now learn how exactly we can do that.

String Length

If you are given a string and you want to know the exact length of the string how are you gonna do that?

We can simply use the length property of the strings which return the length of the string. Let us look at how to write it in the code.

Yes, it is that simple. If you will print len then you will get 18 i.e. the length of the string stored in length.

String Inside String

What if we want to find a substring from a string?

We have various methods to do that. If we want to find out the exact position(index) i.e the first occurrence of the substring we use indexOf() method.

For example if we want to find out the position of “how” in the given string “Hello how are you?”, then we will pass “how” as a parameter in the method indexOf, which will return the first occurrence of the specified string in the index. Let us look at how can we do that-