You are on page 1of 5

Data types – A 

data type specifies the type of data that a variable can store such as


integer, floating, character, etc.

In Other words, Data types specify how we enter data into our programs and what type of
data we enter.

When computer programs store data in variables, each variable must be assigned a
specific data type. Some common data types include integers, floating point
numbers, characters, strings, and arrays. They may also be more specific types,
such as dates, timestamps, boolean values, and varchar (variable character)
formats.
Some programming languages require the programmer to define the data type of a
variable before assigning it a value. Other languages can automatically assign a
variable's data type when the initial data is entered into the variable. For example,
if the variable "var1" is created with the value "1.25," the variable would be
created as a floating point data type. If the variable is set to "Hello world!," the
variable would be assigned a string data type. Most programming languages allow
each variable to store a single data type. Therefore, if the variable's data type has
already been set to an integer, assigning string data to the variable may cause the
data to be converted to an integer format.
Data types are also used by database applications. The fields within a database
often require a specific type of data to be input. For example, a company's record
for an employee may use a string data type for the employee's first and last name.
The employee's date of hire would be stored in a date format, while his or her
salary may be stored as an integer. By keeping the data types uniform across
multiple records, database applications can easily search, sort, and compare fields
in different records.

In programming, data types are an important concept.

To be able to operate on variables, it is important to know something about


the type.

JavaScript provides different data types to hold different types of values. There are
two types of data types in JavaScript.

1. Primitive data type

2. Non-primitive (reference) data type

JavaScript is a dynamic type language, means you don't need to specify type of
the variable because it is dynamically used by JavaScript engine. You need to
use var here to specify the data type. It can hold any type of values such as
numbers, strings etc. For example:

1. var a=40;//holding number  
2. var b="Rahul";//holding string  

Primitive Data
A primitive data value is a single simple data value with no additional
properties and methods.

There are five types of primitive data types in JavaScript. They are as follows:

Data Type Description

String represents sequence of characters e.g. "hello"

Number represents numeric values e.g. 100

Boolean represents boolean value either false or true

Undefined represents undefined value

Null represents null i.e. no value at all

The typeof operator can return one of these primitive types:

 string
 number
 boolean
 undefined

Example
typeof "John"              // Returns "string"
typeof 3.14                // Returns "number"
typeof true                // Returns "boolean"
typeof false               // Returns "boolean"
typeof x                   // Returns "undefined" (if x has no value)

The non-primitive data types are as follows:


Data Type Description

Object represents instance through which we can access members

Array represents group of similar values

RegExp represents regular expression

The typeof operator returns "object" for objects, arrays, and null.

The typeof operator does not return "object" for functions.

Example
typeof {name:'John', age:34} // Returns "object"
typeof [1,2,3,4]             // Returns "object" (not "array", see note
below)
typeof null                  // Returns "object"
typeof function myFunc(){}   // Returns "function"

Difference Between Primitive and Non-Primitive Data


Types in JavaScript
JavaScript primitive data types are data types that refer to a single value.

E.g. var a =5;

Here the variable ‘a’ is an integer data type and has a single integer value. The
variable ‘a’ refers to a single value in memory. If we want to change the value of a,
we would have to assign a new value to a.  Primitive data types are not mutable.

When we create a variable, it reserves a space for itself in the memory. The variable
‘a’ has space in memory which holds its value. When we try to change the value of
‘a’ by assigning another value like var a = 6, it doesn’t alter the value of the original
a, it just creates a new variable ‘a’ with the new value 6.

We can assign the value of ‘a’ to another variable like this:

var a1=a;

Here the variable ‘a1’ is assigned the value of ‘a’, not the address of ‘a’ in memory.

Thus ‘a1’ now holds the same value as ‘a’.


We can compare the two variables ‘a’ and ‘a1’ as the two variables refer to the same
value now.

Using the comparison operator will return a Boolean value of ‘true’.

a===a1 // equals to ‘true’ as ‘===’  checks both the value and type of these two
variables are true.

JavaScript non-primitive types are objects. An object holds a reference/address of a


single key-value pair or many key-value pairs. Whenever we refer to an object, we
refer to an address in memory which contains the key-value pair. If we assign an
object ‘object1’ to another object ‘object2’, we are actually assigning the address of
‘object1’ to ‘object2’ instead of the key-value pair which the ‘object1’ contains in
memory. Let’s see below”.

var object1= {a:5, a1:6};

var object2 = object1;

The above statement assigns the address of object2 to object1, and not the value
{a:5, a1:6}. Thus with this assignment ‘object1’ and ‘object2’ refer to the same
address in memory.

When we compare these two objects, we find that both of them refer to the same
address in memory.

object1===object2 //will return true, as both refer to the same address. If we


compare two separate objects like below:

var object1= {a:5, a1:6};

var object2 = {a:5, a1:6};

This statement object1===object2 // would return a false because both the objects
refer to two separate addresses in memory. When we compare two objects, we
compare their addresses, not their values.

We have seen above in case of primitive data types, that when we assign the
variable ‘a’ to variable ‘a1’, the value of ‘a’ is copied to ‘a1’, and not its address which
happens in non-primitive data types.

Thus primitive data types refer to a ‘single value’ in an address in memory whereas
non-primitive data types refer to the ‘address’ in memory which contains single or
multiple key-value pair/s.

You might also like