What are the data types available in JavaScript?

Data types are a particular type of data item that determines what operations can be applied to them. Once a variable is assigned a data type it can be used for computations in the program.

The best thing about JavaScript is that you really don’t need to define the data type before declaring a variable. Data types exist, but the variables are not bound to any of them; these types of languages are called dynamically typed languages.


Variations of data types

There are 2 kinds of data types, each of which are further divided into sub-types.

  • Primitive data types: Data types which are pre-defined and supported by the programming language.

  • Non-primitive data types: Data types which are created by the user and not pre-defined by the language itself.

svg viewer

Primitive data sub-types


1. Number:

Number type stands for both integers and floating points. Many mathematical operations are carried out using these numbers.

let n = 123;
n=n+10; //mathematical operation on integer (ADDITION)
console.log("Your integer number is : " + n)
n = 12.345;
n=n * 10; //mathematical operation on integer (MULTIPLICATION)
console.log("Your float number is : " + n)

2. String:

Strings are used to store data that involves characters, like names or addresses. You can perform operations like string concatenation, in JavaScript.

let str = "Hello";
let str2 = 'Single quotes are ok too';
let phrase = str + " " + str2; //concatenation of strings
console.log(phrase);

3. Boolean:

Boolean type only has 2 types of return values, true and false. This type is usually used to check if something is correct or incorrect.

let isGreater = 4 > 1;
if (isGreater=true)
console.log("Yes 4 is greater than 1");
else
console,log("No 4 is not greater than 1");

4. Null value:

A null value is used to declare a variable as empty, or as a variable with an unknown value.

let age = null; //since we do not know what the age is..
console.log("age is " + age);

5 Undefined value:

An undefined value is very similar to null value as it also makes a type of its own. The meaning of undefined is “value is not assigned”.

let age = undefined; //since we do not know what the age is..
console.log("age is " + age);

Non-primitive data sub-types


1. Objects:

The definition of objects is created by the user for a defined purpose. The properties of an object define its characteristics. Object properties can be accessed with a simple dot-notation:

var Car = new Object();
Car.make = 'BMW'; //properties
Car.color = 'BLACK';
Car.year = 1999;
console.log("My car was created in : " + Car.year);

2. Arrays:

We use arrays to store data in consecutive memory locations; this helps to access them with ease, and it saves time in searching for the relevant data.

var cars = [" Merc ", " Honda ", " BMW "];
console.log("Our cars are :" + cars); //display an array of cars
Copyright ©2024 Educative, Inc. All rights reserved