DataTypes in JavaScript with examples


In this post, we are going to learn DataTypes in JavaScript with examples. The datatypes in any language are used to specify which type of data we want to store. First, we will understand how javascript is a dynamic type language. We will also learn about typeof operator.

DataTypes in JavaScript

  • Primitive Type
    • string
    • Number
    • Boolean
    • Undefined
    • null
    • Symbol
  • Reference Type/Non-primitive type
    • object
    • Array

JavaScript Dynamic Typing

JavaScript is a loosely typed and dynamic language. The variables in javascript are not associated with any value type. Variable can be assigned or reassigned the value of any type.

The variable is one type when we define, it can change to another type later as in the below example. a is undefined, assign to value 5 change its type to number type, assign string value changes its type to string


let a;
console.log(typeof(a)) //undefined
a = 5;
console.log(typeof(a)) //number
a= "devenum"

console.log(typeof(a)) //string 

JavaScript Typeof

The JavaScript Typeof() operator is used to check the type of a variable. It returns a string indicating the type of the operand. We will use it in examples later in this post.


typeof opeand

Primitive Data Types in JavaScript

1. String Type

The string data type is a collection of characters used to represent the textual data. It is defined by a single quote(”), double quote(” “), backtick character(“) also known as Template string in JavaScript.


let name = 'Dev';
let domain = ""
let greet = `Welcome to devenum
       good morning`




2. Number Type

The number type represents negative or positive integer and floating-point number between -(2^53 − 1) and 2^53 − 1. Javascript number type also includes +infinity, -infinity, NAN. The NAN represents an invalid number. We can check the largest or smallest safe value of a number by using constant Number.MAX_VALUE or Number.MIN_VALUE .

Since ES6(ECMAScript 2015) we can check if the number falls in the double-precision floating-point range by using Number.isSafeInteger()An integer beyond this range is not safe anymore. It will be a double-precision floating-point approximation of the value.


let count = 98.9019;
let num = -98;

  console.log('Precision is safe.')
    console.log('Precision is not safe.')

const val = 42/0;
console.log(typeof(count)) //number
console.log(typeof(num)) //number

console.log(val)  //Infinity
console.log(Number.MAX_VALUE ) //1.7976931348623157e+308 

Example :NAN

let mynum = "hell0"/3
console.log(mynum) //NAN

ECMAScript has two built-in numeric types Number and Bigint.

BigInt type

“The BigInt type is a numeric primitive in JavaScript that represents a whole number greater than 2^53 – 1” The bigint is defined by appending n at end of an integer value. By using constant Number.MAX_SAFE_INTEGER we can find out the largest number represented within the number primitive type.

Example :Bigint

const x = 2n ** 53n;
console.log(typeof(x))  //bigint
console.log(Number.MAX_SAFE_INTEGER) //9007199254740991

3. Boolean Type

The boolean data type represents the logical entity. It has two values true or false. It helps us to control program flow using the if-else, switch statement.


var  a= true;
console.log('you are 18')
console.log('you are not 18')
you are 18.

4. Undefined type

In JavaScript, if a variable is declared and but no value is initialized to that variable then the value of that variable will be undefined.

JavaScript Example

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

5. null

The JavaScript null primitive type has only one value null that represents the absence of any object value. It is used for the special value that is unknown or empty.


const mynum = null;


The Symbol primitive datatype was introduced in ES6. The symbol is created by using the symbol function. We can pass the string into a symbol function for debugging and logging use. The symbol always creates unique value.


let val = symbol('Information logged')
let val2 = symbol('Information logged')

console.log(val == val2); //false

Reference Type/Non-primitive type


The object is a reference data type in javascript. An object type is a complex type that stores a collection of properties with the object literal({}) and property are identified by key-value pair. Where key is string and value can be any datatype or including another object.


var fruits = {
   "name": "apple", "price": 20,  "weight":10,  "quantity": 6

console.log( //apple
console.log(fruits.price) //20
console.log(typeof(fruits)) //object


An array is an object type in javascript that is used to store multiple elements in a single variable of any data type. Each value in the array is correlated with a numeric index. The Index of the array’s first element is starting with 0. We often use an index to access the elements of an array.


let fruits = [
    "apple",  "orange",  "Banana",  "KIWI"

for(let j = 0;j < fruits.length;j++)