JavaScript Data Types

JavaScript Data Types – Overview of JS Types

JavaScript has a variety of data types that are essential to understand, especially for beginners. Here’s an overview of the main data types in JavaScript:


1. Primitive Data Types

Primitive data types are the most basic types of data. They include:

  • String: Used for text. Strings are written inside quotes (" ", ' ', or backticks ` `).
    • Example: "Hello, World!"
  • Number: Represents both integer and floating-point numbers.
    • Example: 42, 3.14
  • Boolean: Represents a logical entity and can have two values: true or false.
    • Example: true, false
  • Undefined: A variable that has been declared but not assigned a value.
    • Example:
      let x;
      console.log(x); // undefined
      

      Try It Now

  • Null: Represents the intentional absence of any object value.
    • Example:
      let y = null;
      console.log(y); // null
      

      Try It Now

  • Symbol: Represents a unique identifier.
    • Example:
      let sym = Symbol('description');
      

      Try It Now

  • BigInt: Used for numbers larger than the Number type can safely handle.
    • Example:
      let bigIntNum = 1234567890123456789012345678901234567890n;
      

      Try It Now

2. Non-Primitive Data Types (Objects)

Objects are collections of properties and methods.

  • Object: Used to store collections of data or more complex entities.
    • Example:
      let person = {
        firstName: "John",
        lastName: "Doe",
        age: 30
      };
      

      Try It Now

  • Array: A special type of object used to store multiple values in a single variable.
    • Example:
      let fruits = ["Apple", "Banana", "Cherry"];
      

      Try It Now

  • Function: A block of code designed to perform a particular task.
    • Example:
      function greet() {
        console.log("Hello, World!");
      }
      

      Try It Now

3. Special Objects

  • Date: Represents date and time.
  • RegExp: Represents regular expressions.
    • Example:
      let pattern = /codeformate/i;
      

      Try It Now

4. Type Checking

You can check the type of a variable using the typeof operator.

  • Example:
    console.log(typeof "Hello"); // "string"
    console.log(typeof 42);      // "number"
    console.log(typeof true);    // "boolean"
    

    Try It Now

Understanding these basic data types and their usage is fundamental to writing and understanding JavaScript code.