JavaScript  

What are the data types in JavaScript?

πŸ” Introduction: Why Data Types Matter in JavaScript

JavaScript is a dynamically typed language, meaning you don’t need to declare variable types explicitly. While this flexibility makes JS beginner-friendly, it also introduces quirks that can trip up even experienced developers.

Understanding JavaScript data types helps you:

  • Write better, bug-free code

  • Avoid confusing type coercion behavior

  • Debug more efficiently

  • Communicate data clearly between APIs, databases, and the browser

Let’s dive deep into data types in JavaScript and how they really work!

🧱 JavaScript Data Types: The Two Main Categories

JavaScript has two categories of data types:

1. Primitive Types (Immutable)

These are basic types with no properties or methods. They are stored directly in memory.

Data Type

Example

String

'Hello', "World"

Number

42, 3.14, -7

Boolean

true, false

Null

null

Undefined

undefined

Symbol (ES6)

Symbol('id')

BigInt (ES11)

12345678901234567890n

2. Non-Primitive (Reference) Types

These are objects and are stored as references.

  • Object { name: "John" }

  • Array [1, 2, 3]

  • Function function() {}

  • Date, RegExp, Set, Map, etc.

πŸ”’ Primitive Types in Detail

πŸ“ String

Used to represent textual data.

let name = "Alice";
console.log(typeof name); // "string"

πŸ”’ Number

Includes integers, floats, and special values like NaN, Infinity.

let count = 10;
let price = 19.99;
console.log(typeof count); // "number"

πŸ” Boolean

Represents a logical entity: true or false.

let isAvailable = false;
console.log(typeof isAvailable); // "boolean"

🚫 Null

Represents intentional absence of any object value.

let data = null;
console.log(typeof data); // "object" (this is a long-standing JavaScript bug!)

πŸ“¦ Undefined

A variable declared but not assigned a value.

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

πŸ” Symbol (ES6+)

Used for unique identifiers.

let id = Symbol('id');
console.log(typeof id); // "symbol"

πŸ”’ BigInt (ES2020+)

For arbitrarily large integers.

let big = 123456789012345678901234567890n;
console.log(typeof big); // "bigint"

πŸ“¦ Object (Reference) Types Explained

Objects in JavaScript can contain key-value pairs and methods.

let person = {
  name: "Bob",
  age: 30
};
console.log(typeof person); // "object"

🧩 Array

Technically, an object with indexed values.

let fruits = ["apple", "banana"];
console.log(typeof fruits); // "object"

βš™οΈ Function

Also an object, but callable.

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

πŸ” JavaScript is Dynamically Typed

You can assign different types to the same variable at different times:

let data = 10;       // number
data = "Ten";        // string
data = true;         // boolean

This dynamic nature is powerful but dangerous. Type-related bugs are common if you’re not careful.

πŸ§ͺ The typeof Operator

Use typeof to check a variable’s type:

console.log(typeof 123);          // "number"
console.log(typeof "hi");         // "string"
console.log(typeof true);         // "boolean"
console.log(typeof {});           // "object"
console.log(typeof []);           // "object"
console.log(typeof function(){}); // "function"
❗ Caution: typeof null returns "object" — a known bug in JavaScript.

🀯 Common Pitfalls to Avoid

  • typeof null === “object” – historical bug, don’t rely on it

  • NaN is a number – typeof NaN === "number"

  • Arrays return "object" – use Array.isArray() to check

  • Loose vs strict equality – == allows type coercion, === doesn’t

0 == "0";     // true
0 === "0";    // false

βœ… Best Practices

  • Prefer === over == to avoid type coercion issues

  • Use let and const instead of var

  • Always initialize variables

  • Avoid mixing types in the same variable

  • Use Array.isArray() for array checks

  • Use TypeScript if strict typing is needed

🧠 Summary

JavaScript offers a lot of flexibility with its types, but that comes with responsibility. Understanding the difference between primitive and reference types, using typeof wisely, and writing type-safe code can help you avoid most runtime surprises.

❓ Your Turn

What’s the weirdest behavior you’ve encountered with JavaScript data types? Share it in the comments and let’s debug together! πŸ›πŸ’¬