JavaScript Data Types

JavaScript is a versatile programming language with various data types categorized into primitive and non-primitive types. Here’s a detailed description with examples:


Primitive Data Types

Primitive data types represent single values and are immutable.

1. Number

The Number type represents both integer and floating-point numbers.

  • 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 is used for textual data, enclosed in single ('), double ("), or backticks (`) for template literals.

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

3. Boolean

The Boolean type represents logical values: true or false.

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

4. Undefined

A variable that has been declared but not assigned a value has the type undefined.

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

5. Null

The null value represents an intentional absence of any 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 is used to handle 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 stores unique values of any type.

  • 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