A web scripting language is called JavaScript. It contains its own data types, much like any other computer language. The type of data that a variable can carry is defined by its data type in a language.

There are eight basic data types in JavaScript.

Data TypesDescriptionExample
StringRepresents textual datalet str = 'Hi', let str2 = "Hello", let str3 = `Hello World`
NumberAn integer or a floating-point numberlet num = 3, let num2 = 3.234, let num3 = 3e-2
BigIntAn integer with arbitrary precisionlet num = 900719925124740999n, let num = 1n
BooleanAny of two values: true or falselet flag = true
undefinedA data type whose variable is not initializedlet a;
nullDenotes a null valuelet a = null;
SymbolData type whose instances are unique and immutablelet value = Symbol('hello');
Objectkey-value pairs of collection of datalet student = { };
Table - Different Data Types in JavaScript

String

Text is stored in strings. Strings in JavaScript are enclosed in quotes:

  • Single quotes: 'Hello'
  • Double quotes: "Hello"
  • Backticks: `Hello`

Example::

// Strings
const firstName = "Code";
const lastName = "Solution";
const result = `Name: ${firstName} ${lastName}`;

console.log(result); // Name: Code Solution

Number

Numerals represent floating-point and integer numbers (decimals and exponentials). Additionally, a number type can be +Infinity, -Infinity, or NaN. (not a number).

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

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

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

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

BigInt

Only numbers less than and greater than -(2sup>53/sup> -1) can be represented using the Number type in JavaScript. However, you can use the BigInt data type if you require a larger number than that.

By adding n to the end of an integer, a BigInt number is produced.

// BigInt value
const num1 = 900719925124740998n;
const num2 = 900719925124740998n;
const num3 = 10;


// Adding two big integers
const result1 = num1 + num2;
console.log(result1); // "1801439850249481996n"


// Error! BitInt and number cannot be added
const result2 = num1 + num2 + num3; 
console.log(result2);  // Uncaught TypeError: Cannot mix BigInt and other types

Boolean

This data type represents logical entities. Boolean values can only be either true or false.

const dataChecked = true;
const valueCounted = false;

undefined

A value that is not assigned is represented by the undefined data type. A variable's value will be undefined if it is declared but its value is not yet assigned.

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

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

null

Null is a special value in JavaScript that denotes an empty or undefined value.

const number = null;

Symbol

Symbol values can be defined as values with the data type Symbol. Symbol is a singular, immutable primitive value.

// Two symbols with the same description

const value1 = Symbol('hello');
const value2 = Symbol('hello');

let result = (value1 === value2) ? true : false;  // false;

// Note: Though value1 and value2 both contain 'hello', they are different as they are of the Symbol type.

Object

A complicated data type called an object enables us to store data collections.

const employee = {
    firstName: 'John',
    lastName: 'K',
    email: '[email protected]'
};

Recommended Posts

View All

What is the let keyword in JavaScript?


Learn all about the let keyword in JavaScript and how it differs from var. Explore its scope, hoisting, and best practices for usage.

What is the difference between Call, Apply and Bind


Looking to learn about the differences between Call, Apply, and Bind in JavaScript? This article breaks down the nuances of each method and provides c...

JavaScript Template Literals Explained


JavaScript template literals are one of JavaScript's most powerful capabilities. They enable us to generate dynamic strings without the need for conca...

How do you decode or encode a URL in JavaScript?


Learn how to encode and decode URLs in JavaScript with this comprehensive guide. Avoid common mistakes and ensure data security. Read more now.

4 Ways to Empty an Array in JavaScript


It is possible to empty an array in a few different ways, so let's go over every method that is available.