JavaScript data types are used to specify what kind of data can be stored and manipulated in a program. JavaScript data types tell the compiler whether the data value is numeric, alphabetic, date, etc. so that it can perform the appropriate operation. Primitive data types can hold only one value at a time and non-primitive data types can hold collections of values and more complex entities.
JavaScript data types are divided into two main categories:
Primitive Data Types:
String
Number
Boolean
Undefined
Null
Non-primitive Data Types:
Object
Date
Array
Primitive Data Types:
The String Data Type:
The data type string is used to represent textual data i.e. sequences of characters and the strings are created using single or double-quotes.
Example:
var a = 'Hello'; // using single quotes
var b = "Learn JavaScript"; // using double quotes
The data type number is used to represent positive or negative numbers with or without decimal place, or numbers are written using exponential notation e.g. 1.5e-4.
Example:
var a = 24.00;
var b = 24;
var c = 2.47;
document.getElementById("trial").innerHTML =
a + "<br>" + b + "<br>" + c;
The Boolean data type holds only two values i.e true or false. It is used to store values like yes (true) or no (false), on (true) or off (false), etc.
Example:
var a = 6;
var b = 7;
var c = 6;
document.getElementById("trial").innerHTML =
(a == b) + "<br>" + (a == c);
The undefined data type can only have one value i.e undefined. If a variable is declared but the value is not assigned to it then it has the value undefined.
In JavaScript, a null value means that there is no value, it is not equivalent to an empty string (“”) or 0, it is simply nothing. It has only one value i.e the null value. A variable can be explicitly emptied of its current contents by assigning it to the null value. In JavaScript, the data type of null is an object.
Emptying an object by setting it to null:
Example:
var person = {firstName:"David", lastName:"Miller", age:30, eyeColor:"brown"};
person = null; //Value is null, but type is still an object
The object is a non-primitive data type that allows you to store collections of data.
An object contains properties and defined as a key-value pair. A property key (name) is always a string, but the value can be any data types, like strings, numbers, booleans, or complex data types like arrays, function and other objects.
Example:
var person = {
firstName : "David",
lastName : "Miller",
technology: "JavaScript"
};
document.getElementById("trial").innerHTML =
person.firstName + " is learing " + person.technology + ".";
An array is object type is used for storing multiple values in a single variable. Each value in an array has a fixed position, known as its index, and it may contain data of any data type-numbers, strings, booleans, functions, objects, and even other arrays. The array index starts from 0 so the first array element is arr[0], not arr[1]. The easiest way to create an array is by specifying the array elements as a comma-separated list enclosed by square brackets.
Example:
var technologies = ["HTML","CSS","JavaScript", "jQuery"];
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok