Data Types Used in Javascript (with Example)

In this tutorial, you will learn about the data types used in javascript with the help of multiple examples.

What are the data types?

Data types are the attributes that specify what kind of data a variable holds. For example, a variable can store different types of data like integer, string, boolean, etc.

We can use different types of data in javascript. For example,

const x = 5;
const y = “hello”;


5 is an integer data.
hello is string data.

Data types in javascript

In JavaScript, there is a total of 8 data types available. They are: 

Data TypesDescriptionExample
Stringrepresents textual data'hii', “hello” etc
Numberrepresents integer and floating-point numbers34, 123e5, etc.
BigIntrepresents an integer of arbitrary length9007199254740991n, 1n, etc.
Booleanrepresents either true or falsetrue and false
undefineduninitialized variable holds an undefined valuelet x;
nullrepresents a null valuelet x = null;
Symbolrepresents a unique identifierlet x = Symbol('hello');
Objectrepresents data as key-value pairslet people = { };

In the above table, all the data types except object are primitive data types, whereas object is a non-primitive data type.

Note: Primitive data types can only hold a single value whereas non-primitive data types can hold multiple values.

Javascript String

String is used to store textual data. In javascript, strings must be surrounded by quotes. For Example, 

let str1 = “Hello”;
let str2 = ‘John’;
let str3 = `$str1}, my name is ${str2}`;
// hello, my name is john

In javascript there are 3 types of quotes:

1. Double quotes: “hello”.

2. Single quotes: ‘hello’.

3. Backticks: `hello`.

Double quotes and Single quotes work the same way so you can use either of them. Whereas Backticks are used to include variables and expressions into a string by wrapping them in ${}.

Javascript Number

Number in javascript represents both integer and floating point numbers (decimals and exponentials), For example,

let x = 5;
let y = 5.6;
let z = 5e2; // 5 * 10^2

Some special values like +Infinity, -Infinity and nan (Not a Number) also belongs to Number data type.

let x = 4/0; // Infinity
let y = -4/0; // - Infinity
let z = “abc”/3; //nan

Javascript BigInt

In javascript, Number type can only represent numbers less than (253 – 1) and more than -(253 – 1). So if you want to represent a number larger than this range then you need to use javascript BigInt data type.

You can create BigInt by appending n at the end of an integer.

const num1 = 909719225124740998n;

const result1 = num1 + 1n;
console.log(result1); // "900719925124740999n"

const num2 = 900719925124740998n;

// Error! BigInt and number cannot be added
const result2 = num2 + 1; 


TypeError: Cannot mix BigInt and other types

Note: BigInt was introduced in the new version of javascript, therefore, some browsers don’t support BigInt. Visit JavaScript BigInt support to learn more.

Javascript Boolean

Boolean data type represents logical entities. Boolean represents one of the two values: true or false. You can think of it as yes and no. Boolean values are generally used in conditional rendering.

let isPresent = true;
let isAbsent = false;

Javascript undefined

Undefined represents the absence of a value. If a variable is declared but not initialized then by default it will hold the value undefined.

Similarly, if a return statement has no value then implicitly it will return undefined.

let name;
console.log(name); // undefined

We can also explicitly assign an undefined value to a variable.

let name = undefined;
console.log(name); // undefined

However, it is not recommended to explicitly assign an undefined value to a variable. Normally, we use null to assign an empty or unknown value to a variable.

Javascript null

null in javascript is used to represent an empty or unknown value.  For example,

In the above code, the number variable has no value.

Javascript Symbol

Javascript Symbol was introduced in the new version of javascript (ECMA2015).

Symbol is a primitive data type in JavaScript. It refers to a unique and immutable symbol value.  We use the Symbol() global factory method to create symbol. For example,

let x = Symbol(‘hello’);
let y = Symbol(‘hello’);

Though x and y contain the same value hello, they are different because they are of symbol type.

Javascript Object

In javascript objects are non-primitive data types. They are mutable and used to store collections of data in key-value pairs. For example,

const student = {
    name: “john”,
    class: 8,
    section: “b”

Javascript type

Javascript is a dynamically typed (loosely typed) language. Javascript automatically determines the type of your variable.

We can also change the type of a variable. For example,

let x = 5; // number type
x = “hello”; // string type
console.log(x); // hello

In the above example, you can see that initially x had number type and then we changed it to string type.

Javascript typeof

In javascript, typeof operator is used to find the type of a variable. For example,

let name = “john”;
typeof(name); // string

let num = 5;
typeof(num); // number

let isTrue = true;
typeof(isTrue); // boolean

let x = null;
typeof(x); // object

Notice that type of null is object type. It is because the data type of null is object type. It is also considered as a bug in javascript.

Leave a Comment

Your email address will not be published. Required fields are marked *