JavaScript is a dynamic, loosely-typed language, which means you don’t have to declare the type of a variable when you create one; it’s determined automatically based on the value assigned. Knowing data types is important because they define how data is stored and what actions you can perform on it.

In this blog, we’ll explore the various data types in JavaScript, categorized as primitive and non-primitive (reference) types.

Primitive Data Types

In JavaScript, the most fundamental building blocks for representing data are known as primitive data types. These types are characterized by their immutability, which means that once a primitive value is created, its underlying value cannot be changed. JavaScript encompasses seven distinct primitive data types:

1. String

A string represents a sequence of characters. It’s used for text .

let name = "Alice";

JavaScript offers flexibility in declaring strings. You can define string literals by enclosing characters within single quotes, double quotes, or using backticks for creating template literals. Each of these methods serves to represent textual data within your code.

let greeting = 'Hello';
let phrase = "What's up?";
let message = `Hi, ${name}`; // Template literal

2. Number

Represents both integers and floating-point numbers.

let age = 25;
let price = 19.99;

JavaScript also supports Infinity, -Infinity, and NaN (Not a Number).

3. Boolean

Represents a truth value and can only be either true or false.

let isLoggedIn = true;
let hasAccess = false;

4. Undefined

A variable that is declared but hasn’t been given a value yet.

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

5. Null

Represents the intentional absence of any value.

let data = null;

typeof null returns “object” a known bug in JavaScript that dates back to its early design.

6. Symbol (ES6)

Used to create unique identifiers for objects. Each Symbol is guaranteed to be unique.

let sym1 = Symbol("id");
let sym2 = Symbol("id");
console.log(sym1 === sym2); // false

7. BigInt (ES2020)

Used for representing integers that exceed the safe range of the Number type.

let bigNum = 1234567890123456789012345678901234567890n;

Non-Primitive (Reference) Data Types

These are more complex data types and are used to store collections of values or more complex entities.

1. Object

The most versatile data type, used to store collections of key-value pairs.

let person = {
  name: "Alice",
  age: 25
};

2. Array

A type of object used for storing ordered collections.

let colors = ["red", "green", "blue"];

3. Function

In JavaScript, functions are considered objects and can be assigned to variables just like other values.

function greet() {
  console.log("Hello!");
}

4. Date, RegExp, Map, Set, WeakMap, WeakSet, etc.

These are special types of objects provided by JavaScript for specific use cases.

Dynamic Typing in JavaScript

JavaScript is dynamically typed, which means variables can change their type during the execution of the program.

let data = 10;       // number
data = "ten";        // now a string
data = true;         // now a boolean

typeof Operator

The typeof operator is a built-in feature in JavaScript that helps identify the type of a variable or expression.

Understanding how typeof works is especially useful in a dynamically typed language like JavaScript, where variables can hold different types of data at different times.

typeof "hello"       // "string"
typeof 42            // "number"
typeof true          // "boolean"
typeof undefined     // "undefined"
typeof null          // "object" (quirky, but true)
typeof {}            // "object"
typeof []            // "object"
typeof function(){}  // "function"

Notes:

While arrays and null both return “object”, you can distinguish them using additional methods like Array.isArray() or strict equality (value === null).

typeof is particularly useful for debugging or handling dynamic data where the type might vary at runtime.

Conclusion

Understanding data types is essential when writing clean and bug-free JavaScript. It helps you make better decisions about how to structure and manipulate your data. Whether you’re building a form, managing API responses, or designing a complex UI component, knowing your data types gives you the confidence to write more predictable and powerful code.

Share this article