JAVASCRIPT DATA TYPES
Data Types in Javascript
Data type defines different types of data stored in any variable. javascript is dynamically typed language means variable data type is defined by language itself. We don't need to explicitly mention the data type of the variable.
A javascript variable can store different types of data over time. Javascript includes primitive and non-primitive data types.
Javascript Primitive Data Type
In javascript the data which are not an object or method are known as primitive data types. There are six primitive data types in javascript:
- Number
- String
- Boolean
- undefined
- null
- symbol
Number
The number
data type represents integer, float, exponential value, hexadecimal, octal and
negative numbers. numbers can be written in exponential and in decimal form in javascript. Example 1, 2, 3, 0,
23.3, 45.02, 12e2, 10e-1 etc.
String
String
is a data type which is used to store texts, character or sentences. It is created by
enclosing text data in single or double quotes.
boolean
boolean
is a data type which has two values true and false. boolean
is used for yes-no, on-off purposes.
undefined
undefined
are the variables which are defined but have not been assigned value to them.
null
null
has only one value, the null. null
is different from
undefined
. null is nor equal to "" neither equal to 0. null
means there is
nothing.
Symbol
Symbol
is another primitive data type. It can be created by calling Symbol(). symbols
are unique every time it is invoked.
Javascript non-primitive Data Type
non-primitive data types are object based or methods. Other data types are called primitive
because they can only hold one type of data but non-primitive
data types can hold different types
of data as a collection. Basically there are three data type in this
- Object
- Array
- Function
Object
The object
is a non-primitive data type; it holds data in the form of key-value pairs. It stores
collections of different types of data.
The property key is always a string, but the value could have any data type.
Here is a simple example to show how to create objects and use them.
Array
Array
is a non-primitive data type which is used to store multiple values in a single variable
which is accessed through index value. Each value stored in the array
has a position value which
starts with 0, known as index of array.
To get array value we write name_of_array[position]. So if name the is
arr, then the first value will be arr[0]
, second value will be arr[1]
and so
on.
Here is an example of an array. We will learn in detail in the coming chapter.
Function
function
is a block of code that can be used for some action multiple times. In javascript
function
can be assigned to some variable. See example below. We will learn about function in
upcoming lessons.
typeof operator
typeof
operator in javascript is an operator which is used to find data type of any variable. To
get data type of variable "a" you can write typeof a
or typeof (a)
.
typeof
operator is used when you need to get data type of some variable to perform some task.
The typeof
operator gives some unexpected results shown below so be careful when using it.