This site uses tracking technologies. You may opt in or opt out of the use of these technologies.

Back to Javascript

Javascript Data Types

JavaScript has several primitive and non-primitive (or complex) data types

Primitive data types

JavaScript has several primitive data types, including:

  1. Numbers: Represents both integers and floating-point numbers. JavaScript only has one type for numbers (64-bit floating-point).

    let age = 30; // integer
    let price = 99.99; // floating-point
  2. Strings: Represents sequences of characters. Strings can be created using single quotes ('), double quotes ("), or backticks (`).

    let name = "Amir";
    let greeting = Hello, ${name}!;
  3. Booleans: Represents logical values: true or false.

    let isLoggedIn = true;
    let hasAccess = false;
  4. Null: Represents an intentional absence of any value. It is often used to indicate "no value" or "empty."

    let result = null;
  5. Undefined: A variable that has been declared but not assigned a value. JavaScript automatically assigns undefined to variables that are declared but not initialized.

    let user;
    console.log(user); // undefined

    Symbol: Introduced in ES6, Symbols are unique and immutable identifiers. They are primarily used for object properties to avoid naming conflicts.

    let id = Symbol("id");

These data types are known as "primitive" because they represent single, immutable values.

Non-primitive (or complex) data types

In JavaScript, non-primitive (or complex) data types refer to structures that can store collections of values or more complex entities. The main non-primitive data types are:

  1. Objects:

    • An object is a collection of key-value pairs, where the keys (properties) are strings (or symbols) and the values can be of any data type (including other objects).

    • Objects can be used to represent real-world entities with properties and methods.

    let user = { 
    name: "Amir",
    age: 30,
    isAdmin: true,
    greet: function() {
    console.log("Hello, " + this.name);
    }
    };
    user.greet(); // Output: Hello, Amir
  2. Arrays:

    • Arrays are special types of objects that are ordered collections of values. Each value in an array is called an element, and elements are accessed via their index.

    • Arrays can store elements of any data type, and the length can change dynamically.

    let colors = ["red", "green", "blue"];
    console.log(colors[1]); // Output: green
  3. Functions:

    • Functions are also considered objects in JavaScript. They are reusable blocks of code that can be called or invoked with arguments.

    • Functions can be assigned to variables, passed as arguments, or returned from other functions.

    function sum(a, b) { 
    return a + b;
    }
    console.log(sum(2, 3)); // Output: 5
  4. Date:

    • The Date object is used to work with dates and times.

    • It provides various methods to get, set, and manipulate dates.

    let today = new Date();
    console.log(today); // Output: Current date and time
  5. Maps:

    • A Map is a collection of key-value pairs where the keys can be of any data type.

    • Unlike objects, Maps preserve the insertion order of their elements, and you can iterate over them easily.

    let map = new Map();
    map.set("name", "Amir");
    map.set("age", 30);
    console.log(map.get("name")); // Output: Amir
  6. Sets:

    • A Set is a collection of unique values. It does not allow duplicate elements.

    • Sets are useful for storing a list of values where duplicates need to be avoided.

    let set = new Set();
    set.add(1);
    set.add(2);
    set.add(2); // Duplicate, will be ignored
    console.log(set); // Output: Set {1, 2}

These non-primitive types provide more complex ways to store and manipulate data compared to primitive types, enabling more flexible programming structures.