JavaScript Data Types

JavaScript is a loosely typed and dynamic language.  In JavaScript you don’t need to specify type of the variable because it is dynamically used by JavaScript engine.

JavaScript automatically determines the variables’ data type for you.

In this tutorial, you will learn about the various data types available in JavaScript with the help of examples.

Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types as shown below.

let foo = 42;    // foo is now a number
foo     = 'bar'; // foo is now a string
foo     = true;  // foo is now a boolean

JavaScript provides different data types to hold different types of values.

There are two types of data types in JavaScript.

  1. Primitive data type
  2. Non-primitive (reference) data type

JavaScript Primitive data types


A string (or a text string) is a series or sequence of characters like “Nil John”, 'hello', "hello world!" etc.

JavaScript’s String type is used to represent textual data. 

Strings are written with quotes. You can use single or double quotes.

let carName1 = "Safari";   // Using double quotes
let carName2 = 'Toyota';   // Using single quotes

You can use quotes inside a string, as long as they don’t match the quotes surrounding the string as shown below.

let answer1 = "It's alright";             // Single quote inside double quotes
let answer2 = "He is called 'Johnny'";    // Single quotes inside double quotes
let answer3 = 'He is called "Johnny"';    // Double quotes inside single quotes

Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on.

The length of a String is the number of elements in it.

You will learn more about  String later in this tutorial.


Number represents integer and floating numbers (decimals and exponentials).

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5

A number type can also be +Infinity-Infinity, and NaN (not a number). 

const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN


Booleans can only have two values: true or false.

It is easier to think of it as a yes/no switch. 

const dataChecked = true;
const valueCounted = false;

Booleans are often used in conditional testing.


The undefined data type represents value that is not assigned.

If a variable is declared but the value is not assigned, then the value of that variable will be undefined.

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

It is also possible to explicitly assign a variable value undefined.

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

Any variable can be emptied, by setting the value to undefined. The type will also be undefined.


It represents null i.e. no value at all.

In JavaScript, null is a special value that represents empty or unknown value.

The Null type has exactly one value: null.

const number = null;

JavaScript non-primitive data types


An object is a complex data type that allows us to store collections of data. 

JavaScript objects are written with curly braces {}.

Object properties are written as name:value pairs, separated by commas.

In JavaScript, objects can be seen as a collection of properties.

const person = {firstName:"John", lastName:"Smith", age:25, eyeColor:"blue"};

The object (person) in the example above has 4 properties: firstName, lastName, age, and eyeColor.

You will learn more about objects later in this tutorial.


It represents group of similar values.

JavaScript arrays are written with square brackets.

Array items are separated by commas.

const cars = ["Saab", "Volvo", "BMW", "Bolero", "Safari"];

Array indexes are zero-based, which means the first item is [0], second is [1], and so on.


It represents regular expression.

We will have great discussion on each data type later in this tutorial.

JavaScript typeof

You can use the JavaScript typeof operator to find the type of a JavaScript variable.

const name = 'ram';
typeof(name); // returns "string"

const number = 4;
typeof(number); //returns "number"

const valueChecked = true;
typeof(valueChecked); //returns "boolean"

const a = null;
typeof(a); // returns "object"

typeof ""             // Returns "string"
typeof "John"         // Returns "string"
typeof "John Doe"     // Returns "string"

typeof 0              // Returns "number"
typeof 314            // Returns "number"
typeof 3.14           // Returns "number"
typeof (3)            // Returns "number"
typeof (3 + 4)        // Returns "number"

You will learn more about typeof later in this tutorial.

Empty Values

An empty value has nothing to do with undefined.

An empty string has both a legal value and a type.

let car = "";    // The value is "", the typeof is "string"