Skip to main content

Introduction to Data Types in JavaScript

In JavaScript, data types define the kind of values you can store and manipulate in your programs. Understanding data types is one of the most important foundations in mastering JavaScript, as it affects how variables behave, how operations work, and how memory is managed.


What Are Data Types?​

A data type represents the kind of value a variable can hold. For example, the number 42, the text "Hello", or a collection like [1, 2, 3] all belong to different data types.

JavaScript is a dynamically typed language, meaning you don’t need to declare the type of a variable. The type is automatically assigned based on the value.

let name = "Ajay"; // String
let age = 24; // Number
let isDev = true; // Boolean

Categories of Data Types​

JavaScript data types are divided into two main categories:

1. Primitive Data Types​

Primitive types are immutable (cannot be changed) and are stored directly in memory.

They include:

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

Example:

let message = "Hello, World!"; // String
let score = 100; // Number
let isOnline = false; // Boolean

2. Non-Primitive (Reference) Data Types​

These are objects that can store multiple values and are stored by reference.

They include:

  • Object
  • Array
  • Function
  • Date, Set, Map, etc.

Example:

let user = { name: "Ajay", role: "Developer" }; // Object
let languages = ["HTML", "CSS", "JavaScript"]; // Array

Why Data Types Matter​

Data types help JavaScript determine:

  • How much memory to allocate.
  • What operations can be performed.
  • How to compare and convert values.

For example:

console.log("10" + 5); // "105" (string concatenation)
console.log("10" - 5); // 5 (numeric subtraction)

Here, JavaScript performs type coercion, converting types automatically when needed.


Type Checking​

To check the data type of a value, you can use the typeof operator:

console.log(typeof "Hello"); // "string"
console.log(typeof 123); // "number"
console.log(typeof true); // "boolean"
console.log(typeof null); // "object" (a known quirk in JS)
console.log(typeof []); // "object"

Summary​

CategoryType ExamplesStored As
PrimitiveString, Number, Boolean, Null, etc.Value
Non-PrimitiveObject, Array, Function, etc.Reference