JavaScript: Data Types
May 20, 2019 | Updated: May 20, 2019

Data types are inseparable part of any programming languages. To perform any operation on variables safely, it is important to know the type of data within the variable.

In this article, I will explain all the available data types in JavaScript.


Dynamically Typed

JavaScript is dynamically or loosely typed language. Type of a variable is determined automatically (dynamically) based on the data within the variable. That means, variables are not directly associated with any specific data type, and so can be changed any time.

let amount; // Now amount is Undefined
console.log(amount); // output: undefined
amount = 100; // Now amount Number
console.log(amount); // output: 100
amount = "one hundred"; // Now amount is String
console.log(amount); // output: one hundred


Data Types

In Javascript, there are two different types of data in general: primitives, and objects. As per the latest ECMAScript standard, JavaScript variable can holds eight different data types of which seven data types are belongs to primitives and rest one is objects.

  • Primitives:

    • Undefined
    • Null
    • Boolean
    • Number
    • BigInt
    • String
    • Symbol
  • Objects


Undefined

In JavaScript, when a variable is declared without a value has the value undefined of type Undefined.

let amount; // the value of amount is undefined
console.log(amount); // output: undefined
console.log(typeof amount); // output: undefined

NB: JavaScript typeof operator is used to find the type of a JavaScript variable.

If you perform any mathematical operation on a variable with undefined value, the result will be NaN which means Not a Number. But if you concatenate a string with undefined value the result will be a string literal.

let amount; // amount is undefined
console.log(amount + 100); // output: NaN

console.log(amount + "Dollar"); // output: undefinedDollar


Null

In JavaScript, Null type has only one value, that is null (means "nothing"). But, surprisingly, in JavaScript, the data type of null value is object instead of Null.

let amount = null;
console.log(amount); // output: null
console.log(typeof amount); // output: object

NB: Remember that, undefined and null are equal in terms of value but different in terms of type.

console.log(typeof undefined); // output: undefined
console.log(typeof null); // output: object

// check equality in terms of value using 'loose equality operator (==)'
console.log(undefined == null); // output: true

// check equality in terms of value and data type using 'strict equality operator (===)'
console.log(undefined === null); // output: false


Boolean

In JavaScript, Boolean type has two values: true and false.

let boolean1 = true;
console.log(boolean1); // output: true
console.log(typeof boolean1); // output: boolean

let boolean2 = false;
console.log(boolean2); // output: false
console.log(typeof boolean2); // output: boolean

console.log(boolean1 == boolean2); // output: false
console.log(typeof boolean1 == typeof boolean2); // output: true


Number

In JavaScript, there is only one Number type, i.e. double precision 64-bits floating point number (with a range between -(253 -1) and 253 -1). More precisely, among the 64 bits, the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63.

So, JavaScript doesn't have different number types like integers, short, long, floating-point, etc. However, numbers can be written with or without a decimal point.

let amount1 = 55.22; // a number with decimal
console.log(amount1); // output: 55.22

let amount2 = 55; // a number without decimal
console.log(amount2); // output: 55

The Number type also have three symbolic values: +Infinity, -Infinity, and NaN (not-a-number). JavaScript will return +Infinity (or -Infinity) if the calculated number cross the limit of the largest (or lowest) number, and NaN is returned when a number is not a legal number. The largest and the smallest value within the +/-Infinity can be checked by using the Number.MAX_VALUE and Number.MIN_VALUE constants.

let amount1 = NaN; // amount1 is NaN
console.log(amount1); // output: NaN
console.log(2 / "zero"); // output: NaN

let amount2 = +Infinity; // amount2 is +Infinity
console.log(amount2); // output: Infinity

let amount3 = -Infinity; // amount3 is -Infinity
console.log(amount3); // output: -Infinity

// Division by zero generates Infinity
console.log(2 / 0); // output: Infinity
// Produce wrong result because beyond the maximum limit in Number
console.log(-2 / 0); // output: -Infinity 

console.log(Number.MAX_VALUE); // output: 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // output: 5e-324

The scientific (exponent) notation can be used to define extra large and extra small numbers in JavaScript.

let amount1 = 55e7; // a number with positive exponent
console.log(amount1); // output: 550000000

let amount2 = 55e-7; // a number with negative exponent
console.log(amount2); // output: 0.0000055

Remember that + is used for both arithmetic addition as well as string concatenation. If two numbers are added the result will be a number, if two strings are added the result will be a string but if a number is added with a string the result will be a string.

let amount1 = 100; // amount1 is 100
let amount2 = 200; // amount2 is 200
let amount3 = "300"; // amount3 is "300"
console.log(amount1 + amount2); // output: 300
console.log(amount1 + amount3); // output: 100300
console.log(amount3 + amount2); // output: 300200
// 100 + 200 + "300" = 300 + "300" = 300300
console.log(amount1 + amount2 + amount3); // output: 300300
// "300" + 200 + 100 = "300200" + 100 = 300200100
console.log(amount3 + amount2 + amount1); // output: 300200100


BigInt

In JavaScript, BigInt is a new primitive data type which can present integers with arbitrary precision. It can store large integers even beyond the safe integer limit for Number type. That means, with BigInt, you can perform operation beyond the Number.MAX_VALUE (safe integer) limit in Number.

// Maximum value in Number Type
console.log(2 ** 53); // output: 9007199254740992
// Produce wrong result because beyond the maximum limit in Number
console.log(9007199254740992 + 1); // output: 9007199254740992 
// Correct result in BigInt
console.log(9007199254740992n + 1n); // output: 9007199254740993n

NB: A BigInt number is created by appending n to the end of the number or by calling the constructor.


String

In JavaScript, String type is used to represent textual data. A string is written with single or double quotes.

let str1 = "hello"; // a string with double quotes
let str2 = 'world'; // a string with single quotes
// String concatenation
console.log(str1 + " " + str2); //output: hello world
console.log(str1.concat(" " + str2)); //output: hello world
// String length
console.log(str1.length); //output: 5

Remember that string can also be defined as objects with the new keyword. In this case it will be a Object type not String type.

let str1 = "hello world"; 
let str2 = new String("hello world"); 
// Checking data type
console.log(typeof str1); //output: string
console.log(typeof str2); //output: object

console.log(str1 == str2); //output: true
console.log(str1 === str2); //output: false


Symbol

In JavaScript, Symbols are new data type introduced in ES6. A Symbol is an immutable primitive and unique value. It can be used as the key of an Object property.


Object

In JavaScript, an object is a collection of properties where properties are defined as key/value pairs, separated by commas. A key value of a property is either a String or a Symbol but a property value can be any type of data, including other objects. In JavaScript, Objects are written with curly braces { }

let myObject = {
    key1: "hello",
    key2: true,
    key3: 55,
    key4: {
        k1: "world",
        k2: false,
        k3: 55.55,
    },
    key5: false,
}
console.log(myObject);
Output:
-------
{ key1: 'hello',
  key2: true,
  key3: 55,
  key4: { k1: 'world', k2: false, k3: 55.55 },
  key5: false }

In JavaScript, all non-primitive types are objects, and hence, arrays and functions are also considered as objects. But the typeof operator returns object type for arrays and objects, and returns function type for functions.

let person = {
    name: "Mr. X",
    age: 55,
};
let city = ["Dhaka", "Munich", "Barcelona",];
function greetings (){ 
    console.log("hello world");
}
// checking type
console.log(typeof person); //output: object
console.log(typeof city); //output: object
console.log(typeof greetings); //output: function


Summary

In a nutshell, there are in total 8 data types in JavaScript of which 7 primitives (undefined, null, boolean, number, bigint, string, and symbol) and objects. However, primitives also can be defined as objects type with their corresponding object representations by using the new keyword. Thought primitive types as objects have nice features like properties and methods, but still for performance primitives should be fast/lightweight.

Happy JavaScript coding!


Comments

You are welcome to write comments, suggestions, corrections, or any queries related to the article. Your comments may take some time to be appeared. Please be aware that any irrelevant comments will be deleted. Thanks for your understanding, and your respectful & relevant comments!