JavaScript Data Types

JavaScript classifies data types into primitive and non-primitive types. Here’s a detailed description with examples:


Primitive Data Types

Primitive data types hold fixed values and cannot be altered.

1. Number

The Number type in JavaScript handles both whole numbers and decimals.

  • Example:
      let age = 25; // Integer
      let price = 99.99; // Floating-point
      let notANumber = NaN; // Special number representing "Not a Number"
      let infinity = Infinity; // Special number for infinity

2. String

The String type stores text and supports ('), double ("), or backticks (`)

  • Example:
      let name = "Alice";
      let greeting = 'Hello';
      let message = `Welcome, ${name}!`; // Template literal with interpolation
    

3. Boolean

The Boolean type holds true or false values for logical operations.

  • Example:
      let isAdult = true;
      let hasPermission = false;
    

4. Undefined

An uninitialized variable in JavaScript has the type undefined.

  • Example:
      let uninitialized;
      console.log(uninitialized); // Output: undefined
    

5. Null

The null type signifies a deliberately empty value.

  • Example:
      let emptyValue = null;
      console.log(emptyValue); // Output: null
    

6. Symbol (ES6)

The Symbol type creates unique and immutable identifiers.

  • Example:
      let uniqueID = Symbol("id");
      console.log(uniqueID); // Output: Symbol(id)
    

7. BigInt (ES11)

The BigInt type is used for very large integers beyond the Number limit.

  • Example:
      let bigNumber = 1234567890123456789012345678901234567890n; // 'n' suffix indicates BigInt
      console.log(bigNumber);
    

Non-Primitive Data Types

Non-primitive types are objects that can store collections of data or more complex entities.

1. Object

Objects are collections of key-value pairs.

  • Example:
      let person = {
      name: "Alice",
      age: 25,
      isStudent: true
      };
      console.log(person.name); // Accessing properties
    

2. Array

An array is a special type of object used to store ordered lists of items.

  • Example:
      let colors = ["red", "green", "blue"];
      console.log(colors[0]); // Accessing the first element
    

3. Function

Functions are first-class objects in JavaScript and can be assigned to variables or passed as arguments.

  • Example:
      function greet(name) {
      return `Hello, ${name}!`;
      }
      console.log(greet("Alice"));
    

4. Date

The Date object allows manipulation and formatting of dates and times.

  • Example:
      let today = new Date();
      console.log(today.toDateString());
    

5. Map

A Map stores key-value pairs with any type of key.

  • Example:
      let map = new Map();
      map.set("name", "Alice");
      map.set("age", 25);
      console.log(map.get("name")); // Output: Alice
    

6. Set

A Set maintains a collection of distinct values without duplicates.

  • Example:
      let set = new Set([1, 2, 3, 2]);
      console.log(set); // Output: Set { 1, 2, 3 }
    

Type Checking

JavaScript has two main ways to check types:

  1. typeof Operator
    Returns the type of a variable.

      console.log(typeof 25); // Output: "number"
      console.log(typeof "Hello"); // Output: "string"
    
  2. instanceof Operator
    Checks if an object is an instance of a specific class.

      console.log([] instanceof Array); // Output: true
                

Previous Next