Datatypes in JavaScript

What is datatypes?

Data types represent different kinds of values that you can work with. JavaScript is a dynamically typed language, meaning you don't need to declare the type of a variable when you define it — the type is automatically determined based on the value assigned.

JavaScript has two categories of data types: primitive types and reference types (also called objects).

1. Primitive Data Types

Primitive data types are basic and immutable, meaning the values cannot be altered. JavaScript has 7 primitive types:

a. Number

Represents both integer and floating-point numbers.

let age = 25;  // Integer
let pi = 3.14; // Floating-point number

Special numeric values:

  • Infinity, -Infinity

  • NaN (Not-a-Number, typically a result of an invalid mathematical operation).

b. String

Represents a sequence of characters enclosed in single ('), double ("), or backticks (` ).

let name = "John Doe";  // Double quotes
let greeting = 'Hello'; // Single quotes
let message = `Hello, ${name}!`;  // Template literals (ES6 feature)

c. Boolean

Represents a logical entity with two values: true or false.

let isActive = true;
let isComplete = false;

d. Undefined

Represents a variable that has been declared but not yet assigned a value.

let x;  // x is undefined

e. Null

Represents the intentional absence of any object value. It's used to explicitly indicate "no value."

let user = null;  // Intentionally empty or "no value"

f. Symbol (ES6)

Represents a unique and immutable value often used for object property keys to avoid naming conflicts.

let symbol1 = Symbol('description');
let symbol2 = Symbol('description');
console.log(symbol1 === symbol2);  // Output: false (symbols are always unique)

g. BigInt (ES11)

Represents integers with arbitrary precision, used when dealing with very large integers that exceed the safe range for regular Number types.

let bigNumber = 123456789012345678901234567890n;  // BigInt

2. Reference (Object) Types

Reference types are mutable and include objects, arrays, functions, and more. These types are stored as references (pointers) to the memory location where the value is stored.

a. Object

Objects are collections of key-value pairs. They can store multiple types of data together.

let person = {
  name: "John",
  age: 30,
  isAdmin: true
};
console.log(person.name);  // Accessing object property

b. Array

Arrays are ordered collections of values. They can hold multiple values of different data types, indexed by position (starting from 0).

javascriptCopy codelet numbers = [1, 2, 3, 4, 5];
console.log(numbers[0]);  // Output: 1 (first element)

c. Function

Functions are callable objects in JavaScript. They can be passed as values and invoked as executable code blocks.

javascriptCopy codefunction greet() {
  console.log("Hello, world!");
}
greet();  // Function invocation

d. Date

The Date object represents dates and times.

javascriptCopy codelet currentDate = new Date();
console.log(currentDate);  // Displays current date and time