Data Types

JavaScript is a dynamically typed language. This means variables are not bound to a specific data type; you can assign a number to a variable and later assign a string to that same variable.

1. The Two Categories

JavaScript data types are divided into two distinct categories: Primitives and Objects.

A. Primitives (The "Atoms")

Primitives are the simplest forms of data. They are immutable, meaning their value cannot be changed once created (though the variable holding them can be reassigned).

There are 7 primitive types:

  1. Number: Floating-point numbers (there is no specific "integer" type in JS).

    • Examples: 42, 3.14, -10.

    • Special values: Infinity, NaN (Not a Number).

  2. String: A sequence of characters.

    • Examples: "Hello", 'World', `Backticks`.

  3. Boolean: Logical entities.

    • Values: true, false.

  4. Undefined: A variable that has been declared but not assigned a value.

    • Value: undefined.

  5. Null: Represents the intentional absence of any object value.

    • Value: null.

  6. BigInt: For integers larger than the Number type can safely represent (2^53 - 1).

    • Example: 9007199254740991n.

  7. Symbol: Unique and immutable identifiers, often used for object keys.

B. Objects (The "Collections")

Objects are collections of properties. Unlike primitives, objects are mutable.

  • Object: { name: "Alice", age: 25 }

  • Array: [1, 2, 3] (Arrays are technically special objects).

  • Function: Code blocks designed to perform tasks (also objects).

  • Date, RegExp, Map, Set: Built-in complex structures.

2. How JavaScript Treats Them: Value vs. Reference

The most important distinction between Primitives and Objects is how they are stored and copied.

Primitives: Passed by Value

When you assign a primitive to a variable, the variable stores the actual value.

Objects: Passed by Reference

When you assign an object to a variable, the variable stores a reference (memory address) pointing to where that object lives in memory, not the object itself.

Because x and y point to the exact same location in memory, changing one affects the other.

3. Type Conversion (Coercion)

Because JavaScript is dynamic, it often converts types automatically. This is called Implicit Coercion. You can also force a change using Explicit Conversion.

A. Explicit Conversion (Manual)

This is when you tell JS exactly what you want. It is safer and more readable.

B. Implicit Coercion (Automatic)

This happens when you apply operators to mismatched types.

1. String Concatenation wins over Math

If you use + and one side is a string, JS converts the other side to a string.

2. Math wins with other operators

If you use -, *, /, or %, JS tries to convert strings to numbers.

3. Equality: == vs ===

  • Double Equals (==): Allows coercion. It converts types to match before comparing.

    • 5 == "5" is true.

    • 0 == false is true.

    • null == undefined is true.

  • Triple Equals (===): Strict equality. No coercion allowed. Always use this.

    • 5 === "5" is false.

4. Truthy and Falsy Values

When a value is expected to be a Boolean (like in an if statement), JS converts it.

Falsy Values (There are only 6):

  1. false

  2. 0 (and -0)

  3. "" (empty string)

  4. null

  5. undefined

  6. NaN

Truthy Values:

Everything else.

  • "0" (non-empty string) -> true

  • [] (empty array) -> true

  • {} (empty object) -> true

  • function(){} -> true

5. The "Gotcha" of Typeof

You can check a variable's type using the typeof operator, but there are quirks.

Note: typeof null returning "object" is a historical bug in JavaScript that cannot be fixed without breaking existing code on the internet. null is actually a primitive.

Last updated