JavaScript Data Types
JavaScript includes data types similar to other programming languages like Java or C#. JavaScript is dynamic and loosely typed language. It means you don't require to specify a type of a variable. A variable in JavaScript can be assigned any type of value, as shown in the following example.
var myvariable = 1; // numeric value
myvariable = 'one'; // string value
myvariable = 1.1; // decimal value
myvariable = true; // Boolean value
myvariable = null; // null value
In the above example, different types of values are assigned to the same variable to demonstrate loosely typed characteristics of JavaScript.
Different values 1
, 'one'
, 1.1
, true
are examples of different data types.
JavaScript includes primitive and non-primitive data types as per latest ECMAScript 5.1.
Primitive Data Types
The primitive data types are the lowest level of the data value in JavaScript. The typeof operator can be used with primitive data types to know the type of a value.
The followings are primitive data types in JavaScript:
Data Type | Description |
---|---|
String | String is a textual content wrapped inside ' ' or " " or ` ` (tick sign).Example: 'Hello World!', "This is a string", etc. |
Number | Number is a numeric value. Example: 100, 4521983, etc. |
BigInt | BigInt is a numeric value in the arbitrary precision format. Example: 453889879865131n, 200n, etc. |
Boolean | Boolean is a logical data type that has only two values, true or false. |
Null | A null value denotes an absense of value. Example: var str = null;
|
Undefined | undefined is the default value of a variable that has not been assigned any value. Example: In the variable declaration, var str; , there is no value assigned to str .
So, the type of str can be check using typeof(str) which will return undefined .
|
Structural Data Types
The structural data types contain some kind of structure with primitive data.
Data Type | Description |
---|---|
Object | An object holds multiple values in terms of properties and methods. Example:
|
Date | Date object represents date & time including days, months, years, hours, minutes, seconds and milliseconds. Example: var today = new Date("25 July 2021");
|
Array | An array stores multiple values using special syntax. Example: var nums = [1, 2, 3, 4];
|
Learn about each data type in detail in the next section.