Javascript Data Types

A value in JavaScript is always of a certain type, that type is known as data type. Javascript provides different data types to hold different types of values.

var a=20;//holding number  
var b="Test";//holding string  

There are two types of data types supported by JavaScript:-

Primitive data type

A primitive data value is a single simple data value with no additional properties and methods. There are five types of primitive data types in JavaScript.

Data TypeDescription
Stringrepresents sequence of characters e.g. “hello”
Numberrepresents numeric values e.g. 100
Booleanrepresents boolean value either false or true
Undefinedrepresents undefined value
Nullrepresents null i.e. no value at all

JavaScript is a dynamic language or loosely typed therefore a variable doesn’t associate with any type. However, its value does.

In other words, the same variable can hold values of different types at any time. For example:

let counter = 20; // counter is a number
counter = false;   // counter is now a boolean
counter = "test";   // counter is now a string 

The string type

In JavaScript, a string is a sequence of zero or more characters. A literal string begins and ends with either a single quote(‘) or double quote (“).

A string that starts with a double quote must end with a double quote and a string that begins with a single quote must end with a single quote.

let greeting = 'Hi';
let s = "It's a valid string";
let str = 'I\'m also a string'; // use \ to escape the single quote (') 

JavaScript strings are immutable. It means that we cannot modify a string once it is created.

However, we can create a new string based on an operation on the original string, like this:

let str = 'JavaScript';
str = str + ' String'; 

The number type

In JavaScript, a variable can hold number of any type – integer or floating.

To represent a floating-point number, we include a decimal point followed by at   least one number and integer number is without any decimal point.

let f1 = 12; //Integer number
let f2 = 0.3; //Floating point number

JavaScript converts a floating-point number into an integer number if the number appears to be the whole number.

The reason is that Javascript always wants to use less memory since a floating-point value uses twice as much memory as an integer value.

The boolean type

The boolean type has two values: true and false, in lowercase. It allows values of other types to be converted into boolean values of true or false.

To convert a value of another data type into a boolean value, we use the Boolean function.

let inProgress = true;
let completed = false;

The undefined type

The undefined type is a primitive type that has one value undefined.

By default, when a variable is declared but not initialized, it is assigned the value undefined.

let counter;
console.log(counter);        // undefined
console.log(typeof counter); // undefined

In the above example, the counter is a variable. Since counter hasn’t been initialized, it is assigned the value undefined. The type of counter is also undefined.

The null type

The null type is the second primitive data type that also has only one value: null.

It defines that null is an empty object pointer.

let obj = null;
console.log(typeof obj); // object

It is a good practice to assign a variable that later holds an object to null so that you can check whether the object is null or not by using the if statement.

Non-primitive (reference) data type

The non-primitive data types are as follows:

Data TypeDescription
Objectrepresents instance through which we can access members
Arrayrepresents group of similar values
RegExprepresents regular expression

The object type

In JavaScript, an object is a collection of properties, where each property is defined as a key-value pair.

The following example defines the person object with two properties.

let person = {
    firstName: 'John',
    lastName: 'Doe'

A property name of an object can be any string. We can use quotes around the property name if it isn’t a valid JavaScript identifier.

The array type

JavaScript arrays are used to store multiple values in a single variable.

var person = ["Matt", "Jenny", "Mike"];

A property name of an object can be any string. We can use quotes around the property name if it isn’t a valid JavaScript identifier.

The regExp type

RegExp is a regular expression in which sequence of characters used that forms a search pattern.

That search pattern can be used for text search and text replace operations.

let re = new RegExp('hi');