What are the data types used in JavaScipt

Interview Question

Table of contents

No heading

No headings in the article.

There are 2 types of data types

  1. Primitive data type

  2. Non-Primitive Data type

Primitive Data type: Primitive Data types are those whose data is not an object or a method or properties

Following are the types of Primitive Data type:

  1. String

  2. Number

  3. bigInt

  4. Boolean

  5. Undefined

  6. Null

  7. Symbol

String : In any Computer Programming Language,a string is a sequence of characters used to represent text.String can be defined with quotes or without quotes Example :

var str = 'This is also a string';
var str 2 = "This is also a string";

Number : It represent a number and can be written with or without decimal

var x = 2; // without decimal
var y = 2.2; // with decimal

BigInt : In JavaScript ,it used to store numbers which are above the limitation of the Number Data Type .It can store large integers and is represented by adding "n" to an interger literal.

var bigInteger = 123456789012345678901234567890n;

Boolean : It represent a logical entity and can have only two values either true or false.Booleans are used for conditional testing.

Example:

var a = 2;
var b =3;
var c = 2;
(a==b) // it will give us false
(a==c) // it will give us True

Undefined : When a variable is declared but not assigned any value then JavaScript Engine will set that value as a undefined and its type also undefined

Example :

var x ; // value of x is undefined
var y = undefined; // we can set undefined as value also

Null : In JavaScript,it represent a non-exist or invalid value.

Example:

var z = null;

Symbol:It is a new Data type introduced in ES6 version of JavaScript.Its used to store an anonymous and unique value.

Example :

var symboll = Symbol('symbol');

Non-Primitive Data Type:

Primitive data type can store only a single value but Non-Primitive data type can store mutiple and complex values.

1 .Object : Used to store collection of data Example:

var obj1 = {
    x:43, // number
    y:'Hello', // string
    z:function(){
        return this.x; 
    }
}

Array : The Array enables storing a collection of multiple items under a single variable name.

Example :

var arr = [5,"hello",true,4.1]; // can store different data type value in single variable