Tutorials Tonight's Logo
Follow Us


Data Types in Javascript

Data type defines different types of data stored in any variable. javascript is dynamically typed language means variable data type is defined by language itself. We don't need to explicitly mention the data type of the variable.

// Dynamically Typed
let x = 10; // can store a number
x = "javascript"; // now x is a String
x = true; // now x is a boolean

A javascript variable can store different types of data over time. Javascript includes primitive and non-primitive data types.

Javascript Primitive Data Type

In javascript the data which are not an object or method are known as primitive data types. There are six primitive data types in javascript:

  • Number
  • String
  • Boolean
  • undefined
  • null
  • symbol


The number data type represents integer, float, exponential value, hexadecimal, octal and negative numbers. numbers can be written in exponential and in decimal form in javascript. Example 1, 2, 3, 0, 23.3, 45.02, 12e2, 10e-1 etc.

let int_type = 10; // Integer
let float_type = 15.55; //Float
let exponential_type = 12.2e2; // exponential 12.2e2 = 1220
let hexadecimal = 0x15f; // hexadecimal 0x15f = 351
let octal = 020; // octal 020(octal) = 16 (decimal)


String is a data type which is used to store texts, character or sentences. It is created by enclosing text data in single or double quotes.

let lang = 'javascript'; // using single quote
let line = "Hello, World!"; // using double quote
▶ Run the code


boolean is a data type which has two values true and false. boolean is used for yes-no, on-off purposes.

let x = true;
let y = false;


undefined are the variables which are defined but have not been assigned value to them.

let a; // currently undefined
a = 10; // now defined a = 10


null has only one value, the null. null is different from undefined. null is nor equal to "" neither equal to 0. null means there is nothing.

let a = null; // null value
let b; // undefined
document.getElementById("output1").innerHTML = a==b; // false
document.getElementById("output2").innerHTML = a==0; // false
▶ Run the code


Symbol is another primitive data type. It can be created by calling Symbol(). symbols are unique every time it is invoked.

document.getElementById("output").innerHTML = Symbol()==Symbol();
// false (every time symbol is invoked it is different)
▶ Run the code

Javascript non-primitive Data Type

non-primitive data types are object based or methods. Other data types are called primitive because they can only hold one type of data but non-primitive data types can hold different types of data as a collection. Basically there are three data type in this

  • Object
  • Array
  • Function


The object is a non-primitive data type; it holds data in the form of key-value pairs. It stores collections of different types of data.

The property key is always a string, but the value could have any data type.

Here is a simple example to show how to create objects and use them.

var student = {
  "name": "clark", // value string
  "age": 21 // value number
▶ Run the code


Array is a non-primitive data type which is used to store multiple values in a single variable which is accessed through index value. Each value stored in the array has a position value which starts with 0, known as index of array.

To get array value we write name_of_array[position]. So if name the is arr, then the first value will be arr[0], second value will be arr[1] and so on.

Here is an example of an array. We will learn in detail in the coming chapter.

var fruits = ['Mango', 'Banana', 'Orange', 'Guava'];
document.getElementById("first-value").innerHTML = fruits[0];
document.getElementById("second-value").innerHTML = fruits[1];
▶ Run the code


function is a block of code that can be used for some action multiple times. In javascript function can be assigned to some variable. See example below. We will learn about function in upcoming lessons.

var myFunction = function(){ // Defining function
  alert("function called"); // block of code
myFunction(); // calling function
▶ Run the code

typeof operator

typeof operator in javascript is an operator which is used to find data type of any variable. To get data type of variable "a" you can write typeof a or typeof (a).

typeof operator is used when you need to get data type of some variable to perform some task.

The typeof operator gives some unexpected results shown below so be careful when using it.

var str = "food"; // String
var num = 10; // Number
var bool = true; // boolean
var undf; // undefined
var nl = null; // null
var obj = { "id": 121 }; // object
var symb = Symbol(); // symbol
var arr = ["mango", "banana", "orange"]; // array
var fxn = function () { }; // function

console.log(typeof str); // output: String
console.log(typeof num); // output: Number
console.log(typeof bool); // output: boolean
console.log(typeof undf); // output: undefined
console.log(typeof nl); // output: object
console.log(typeof obj); // output: Object
console.log(typeof symb); // output: Symbol
console.log(typeof arr); // output: Object
console.log(typeof fxn); // output: function

// Some other direct values
console.log(typeof 10); // output: Number
console.log(typeof Math.PI); // output: Number
console.log(typeof Infinity); // output: Number
console.log(typeof NaN); // output: number
console.log(typeof undefined); // output: undefined
▶ Run the code

More Learning

Want to learn more about programming and web development? We have a few recommendations for you.

Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.