Everything About JavaScript Data Types

·

3 min read

In JavaScript, data types define the kind of value that can be stored and manipulated. JavaScript is a dynamically typed language, meaning variables can hold values of any data type, and the type can change at runtime.


JavaScript Data Types

JavaScript has two main categories of data types:

  1. Primitive Types

  2. Reference Types


1. Primitive Types

These are immutable (cannot be changed) and directly represent a single value.

Primitive Data Types:

  1. Number:

    • Represents both integers and floating-point numbers.

    • Example:

        let age = 25;
        let price = 19.99;
      
  2. BigInt:

    • Used to represent integers larger than Number.MAX_SAFE_INTEGER (2^53 - 1).

    • Example:

        let largeNumber = 1234567890123456789012345678901234567890n;
      
  3. String:

    • Represents text, enclosed in single ('), double ("), or backticks (` ).

    • Example:

        let name = 'John';
        let greeting = `Hello, ${name}`;
      
  4. Boolean:

    • Represents true or false.

    • Example:

        let isLoggedIn = true;
      
  5. Undefined:

    • A variable that has been declared but not assigned a value.

    • Example:

        let result;
        console.log(result); // undefined
      
  6. Null:

    • Represents an intentional absence of any value.

    • Example:

        let data = null;
      
  7. Symbol:

    • Represents a unique, immutable value, often used as property keys.

    • Example:

        let id = Symbol('uniqueId');
      

2. Reference Types (Objects)

These are mutable and can store collections of values or more complex entities.

Reference Data Types:

  1. Object:

    • A collection of key-value pairs.

    • Example:

        let user = { name: 'Alice', age: 30 };
      
  2. Array:

    • An ordered list of values.

    • Example:

        let colors = ['red', 'green', 'blue'];
      
  3. Function:

    • A block of reusable code.

    • Example:

        function greet() {
          console.log('Hello!');
        }
      
  4. Date:

    • Represents dates and times.

    • Example:

        let today = new Date();
      
  5. RegExp:

    • Represents regular expressions for pattern matching.

    • Example:

        let pattern = /abc/;
      
  6. Map, Set, WeakMap, WeakSet:

    • Special objects for key-value storage (Map) or unique values (Set).

    • Example:

        let map = new Map();
        let set = new Set();
      

Special Considerations

Dynamic Typing:

A variable's type can change at runtime:

let value = 42;        // Number
value = 'Hello';       // String
value = true;          // Boolean

Type Checking:

Use typeof to check the type of a value:

console.log(typeof 42);          // "number"
console.log(typeof 'Hello');     // "string"
console.log(typeof true);        // "boolean"
console.log(typeof null);        // "object" (a known quirk in JavaScript)
console.log(typeof undefined);   // "undefined"

Primitive vs. Reference Types

FeaturePrimitive TypesReference Types
StorageStored in the stack.Stored in the heap.
MutabilityImmutable (cannot be changed).Mutable (can be modified).
CopyingCopied by value.Copied by reference.

Example:

// Primitive
let a = 10;
let b = a;
b = 20;
console.log(a); // 10 (a is not affected)

// Reference
let obj1 = { name: 'Alice' };
let obj2 = obj1;
obj2.name = 'Bob';
console.log(obj1.name); // "Bob" (obj1 is affected)

Summary

  • JavaScript has 7 primitive types: Number, BigInt, String, Boolean, Undefined, Null, and Symbol.

  • All other types are reference types, primarily Object and its variations (e.g., Array, Function).

  • Use typeof to check types and understand whether a value is primitive or reference.