Professional Documents
Culture Documents
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.
JavaScript provides different data types to hold different types of values. There are
two types of data types in JavaScript.
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:
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)
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"
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.
var a1=a;
Here the variable ‘a1’ is assigned the value of ‘a’, not the address of ‘a’ in memory.
a===a1 // equals to ‘true’ as ‘===’ checks both the value and type of these two
variables are true.
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.
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.