Signup/Sign In

JavaScript Data Types

In JavaScript, any value that you may use belongs to a Data type. There are several different types of data allowed in JavaScript. You can assign data to variables in JavaScript, but the variables are not

When you create a variable in JavaScript, you do not have to worry about what type of data you will store in it because JavaScript is a Dynamically Typed language (we studied this in JavaScript features) so you don't even have to specify the data type for the variable while declaring it.

A dynamically typed language is a language that infers data types at runtime. It allows storing different types of values in a variable.

Below we have a basic example, where we have defined a variable with a numeric value and then we updated it to store a string value. Yes! JavaScript allows this.

let x = 5;    // x is a number
x = "studytonight";    // x is string here.

In JavaScript, Data types can be categorized into 2 different categories, they are:

  • Primitive datatypes

  • Composite datatypes (Non-primitive)

Let's see which data types fall into these categories.

Primitive Data Types in JavaScript

JavaScript Primitive data types can be classified further into the following types:

  1. String

  2. Boolean

  3. Number

  4. Undefined

  5. Null

  6. Symbol

  7. BigInt

These are the most commonly used data types. Symbol and BignInt are new additions to this list.

Symbol was introduced in ECMAScript2015 or ES6, whereas BigInt was added to JavaScript as part of ECMAScript2020 updates.

Let's see what type of values these datatypes support with code examples.

1. String Data type in JavaScript

Whenever we write a character or a sequence of characters or numbers or special characters, or anything inside single or double quotes then it becomes a String. For example "StudyTonight", "1234", "STU$%12", etc.

We can use both single or double quotes to create string values.

let str = "Ferari F60";  // string using double quotes

let str1 = 'Volvo S60';  // string using single quotes

To have a single quote as part of the string we should use the double quote to enclose the string value and vice versa.

If you want to include a single quote in the string that is enclosed inside single quotes, in that case, we must use a backslash \ to escape the single quote, and similarly, we can escape the double quotes too in a string value.

Let's see an example of this:

let str1 = "Ferari's F60";    // Output: Ferari's F60

let str2 = 'Volvo "S60"';   // Output: Volvo "S60"

let str3 = 'Ferari\'s F60';    // Using escape character backslash

We have covered JavaScript String in detail along with the various String methods in JavaScript.

2. Boolean Data type in JavaScript

The boolean type can have either true or false as a value. Boolean values are used when you need some value as a flag in your code, or in the case of conditional statements, etc.

let isOn = true;  // bulb is on

let isOn = false;  // bulb is off

We get Boolean values while comparing two numbers, for example:

console.log(4 < 2)  // false
console.log(4 > 2)  // true

We will see this in the JavaScript If Else tutorial.

3. Number Data type in JavaScript

Number data type means all numeric values, be it integer values, or fractional values. Integer values are whole numbers, and fractional values are valued with decimal points.

let x = 45; // Number without decimal point

let y = 45.90; // Number with decimal point - floating point

let z = -10; // Number with negative value

The Number data type in JavaScript also represents some special values like Infinity, -Infinity and Nan. When a positive number is divided by zero (it's a popular case of runtime error), in JavaScript it's represented as Infinity. Similarly, when a negative number is divided by zero we will get -Infinity.

let a = 100;
let b = -100;
let c = 0;

console.log(a/c);  // Infinity 
console.log(b/c);  // -Infinity

And Nan means Not a number, if we try to perform any operation between a numeric value and a non-numeric value like a string we will get this as output.

let a = "Studytonight";
let b = 7;

console.log(a/b);    // Nan

4. Undefined in JavaScript

When a variable is declared but not assigned any value, its value is undefined. Yes, undefined is a valid data type in JavaScript and it can have only one undefined value.

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

We can even use the undefined value while doing comparisons. Let's take an example:

let a;
console.log(a == undefined);   // true

Notice that we haven't enclosed the undefined value in quotes. If you enclose it within a single or double quote ('undefined' or "undefined"), then it is treated as a string value.

5. Null in JavaScript

The null value is used to represent no value. It is neither similar to undefined, nor equal to empty value or zero value. The null datatype means, no value. You can assign a null value to a variable.

The Null data type can have only one value, which is null. Let's take an example of this:

let a = null;

console.log(a);    // Output will be null

6. Symbol in JavaScript

To generate unique values in JavaScript, Symbol can be used. The symbol is used to generate a unique identifier. You can use it to generate a unique key for an object.

To create a Symbol value, you can use the Symbol() function, and every time you call it, it will return a unique symbol value.

// Creating symbols
const symbol1 = Symbol();
const symbol2 = Symbol("Some Description");  // Optional description

// Symbols are unique
console.log(symbol1 === symbol2);  // false

// Symbols as object keys
const obj = {
    [symbol1]: 'value'

console.log(obj[symbol1]);  // 'value'

7. BigInt in JavaScript

The BigInt type values are used beyond the limit of the Number type. Yes, the maximum value that the Number datatype can hold is 2^53 - 1 or Number.MAX_SAFE_INTEGER or 9007199254740991, beyond this value, you cannot store the value as a Number type.

To create a BigInt type value, all you have to do is add 'n' at the end of the number literal. Let's see an example,

const bigIntValue = 9007199254740991n;  // Note the 'n' at the end

Composite Data Types in JavaScript

These data types can hold collections of values and more complex values.

  1. Object type

  2. Array type

1. Object in JavaScript

In JavaScript, an object is used to store a collection of data. An object is a collection of key:value pairs, where pairs of key-value are separated by commas and enclosed within curly braces {}.

The key (name) must always be a string, but the value can be of any data type. Let's see a simple example,

let name = {};   // It will create an empty object.

let emp = {
    firstname : "Ram", 
    lastname : "Singh", 
    salary : 20000, 
    insured : true

We have covered JavaScript Objects in a separate tutorial with examples.

2. Array in JavaScript

An array is a collection of values or a list of values. The values can be of the same type or of different types. To create an array in JavaScript, you can use square brackets [] and inside the square brackets, you can specify the comma-separated list of values.

Each element in the array gets a numeric position, known as its index. The array index starts from 0, so that the first array element is arr[0] and not arr[1].

Let's take an example of a JavaScript array:

// Creating an Array
var cars = ["Ferrari", "Volvo", "BMW", "Maseratti"];

We have covered JavaScript Arrays in a separate tutorial with examples.

JavaScript typeOf Operator

The typeOf operator in JavaScript can be used to check the data type of any value.

Here are a few examples to see how this works:

let a = null;
console.log(typeOf a);   // null

// Array datatype
let cars = ["Ferrari", "Volvo", "BMW", "Maseratti"];
console.log(typeOf cars);    // array

You should try the typeOf operator with values of different data types.

About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight