Data Types in Javascript
Table of Content:
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
Primitive JavaScript data types:
- String [ "Howdy"]
- Number [ 23, 482.038]
- Boolean [ true, false ]
- Null [ null value ]
- Undefined [ value not defined ]
- Objects
Examples:
-
Numbers
are only double precision 64-bit values. There are no integers in JS. For example – 42 or 42.39794 -
Strings
are to store characters. -
Booleans
are true and false. Undefined
is unique to JavaScript. It is a data type assigned to variables that are declared as variables but not assigned any values.-
Null
is a type to denote null value.
var length = 16; // Number var lastName = "Ansari"; // String var x = {firstName:"Rambo", lastName:"Azmi"}; // Object
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:
var a=80;//holding number var b="Rambo";//holding string
Overview
Data Type | Description |
---|---|
String | represents sequence of characters e.g. "Rambo" |
Number | represents numeric values e.g. 120 |
Boolean | represents boolean value either false or true |
Undefined | represents undefined value |
Null | represents null i.e. no value at all |
JavaScript non-primitive data types
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 |
We will have great discussion on each data type later.
Primitive data types does not have any methods.
JavaScript provides wrapper objects for the primitive datatypes with methods implemented.
Primitive Datatype | Wrapper Object |
string | String |
number | Number |
boolean | Boolean |
symbol | Symbol |
For the primitive datatypes : null, undefined : there are no Wrapper Objects.