Important Data Types in JavaScript

Data type means the type of data which can be stored and used in a program. There are 6 data types in JavaScript. There are mainly three categories which data types can be divided into:

  • Primitive Data Types – This includes string, number and boolean. It holds only one value at a time.
  • Reference Data Types – This includes function, object and array. It holds collection of values.
  • Special Data Types – This includes null and undefined.

String Data Type
String data type represents a group of characters.

var name = "Mike"; //Creating variable as string data type
var walk = "Let's go for a walk";

Number Data Type
Number data type represents positive or negative number and also include the decimal and exponential notations values as well.

var num = 100;
var num_1 = 25.5;

If you add number and string, number will be treated as string. JavaScript simplifies the expression form left to right order as well.

var num = 100;
var h = 100 + "myname"; //Output:  100myname
var new = 10 + 2 + "add"; //Output: 12add
var try = "newstr"+ 10 + 2 ; //Output: newstr102

Boolean Data Type
Boolean can either be true or false. You can see in the example below that when compared i and j it returns false because comparison operators also results in boolean data type.

var accept = false; //sets boolean to false
var  i = "i";
var j = "j"; 
console.log(i == j) //Output: false

Array Data Type
Array stores the multiple values in one variable. Each value stored in an array has index which you can use it while accessing the data.

var car = ["hyundai", "honda","toyota","bmw"];
//Index in an array starts from 0.
for(var i=0;i< car.length;i++){
   console.log(car[i]) //where i represents the index position into an array.
}

 //Output: "hyundai""honda""toyota""bmw"

Object Data Type
Object data type helps you to have a collection of data as key and value pairs. You can see in the below example that we have created a collection of car data instead of storing the data into different variables.

var car = { year : 2017 , model : "Elantra", brand : "hyundai" , used: false }

Function Data Type
Function executes a block of code. A function can also be assigned to a variable because it is an object. Function needs to defined with keyword “function”. function needs to be called in order to be executed.

function welcome(full_name){
  return "Welcome" + full_name;
}
welcome("Mike Doe"); //returns "Welcome Mike Doe"

//Assigned to a variable
var add = function(a,b){
  return a + b;
}

// function call
add(10,10); //returns 20

To ready about null and undefined. Please click here.

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top