13 Feb 2024




Beginner

JavaScript has several data types, which can be categorized into two main categories: primitive data types and non-primitive (reference) data types.

  1. Primitive Data Types: Primitive data types are immutable (i.e., once they are created, their values cannot be changed) and hold a single value. They are directly stored in memory and accessed by their value. Primitive data types are passed by value.
    • Number: Represents numeric values, including integers and floating-point numbers.
      • Range of the number data type:
        • Minimum Value: -Number.MAX_VALUE (-1.7976931348623157e+308)
        • Maximum Value: Number.MAX_VALUE (1.7976931348623157e+308)
        • Integer Range: -2^53 + 1 to 2^53 - 1 (-9007199254740991 to 9007199254740991)
        • Floating-Point Precision: Approximately 15 decimal digits of precision
      • JavaScript has special numeric values like Infinity (positive infinity), -Infinity (negative infinity), and NaN (Not-a-Number). which are part of the number data type but do not fall within the traditional numeric range.
     let num = 10;
    
    • String: Represents textual data, enclosed within single quotes (''), double quotes ("") or backticks (``).
      let str = "Hello, world!";
    
    • Boolean: Represents a logical value, which can be either true or false.
      let isTrue = true;
    
    • Undefined: Represents a variable that has been declared but not assigned a value, or a function that returns no value.
      let undefinedVar;
    
    • Null: Represents the intentional absence of any object value.
      let nullVar = null;
    
    • Symbol: Introduced in ECMAScript 6, represents a unique identifier.
      const symbol = Symbol('description');
    
    • BigInt: Introduced in ECMAScript 2020, represents integers with arbitrary precision.
      const bigIntNum = 9007199254740991n;
    

💡Note: Primitive data types (like numbers, strings, and booleans) are immutable. Once they are created, their values cannot be changed. For example, if you have a string "hello", you cannot change it to "world". Instead, you create a new string with the desired value.

⭐Note: Remember, each time you "change" the value of a primitive data type, you are actually creating a new value and assigning it to the variable. This is because primitive data types are immutable in JavaScript.

  1. Non-primitive (Reference) Data Types: Non-primitive data types are mutable (i.e., once they are created, you can modify the properties and elements of these objects directly) and hold references to values, rather than the values themselves. They are stored as references in memory. Non-primitive data types are passed by reference.
    • Object: Represents a collection of key-value pairs. Objects can contain primitive data types, other objects, and functions.
    let person = { name: 'John', age: 30 };
    
    • Array: A special type of object used to store multiple values in a single variable.
    let numbers = [1, 2, 3, 4, 5];
    
    • Function: A callable object that executes a block of code.
    function greet(name) {
      console.log(`Hello, ${name}!`);
    }
    
    • Date: Represents a date and time value.
    let currentDate = new Date();
    
    • RegExp: Represents a regular expression object used for pattern matching within strings.
    let pattern = /[a-zA-Z]+/;
    
    • Map, Set, WeakMap, WeakSet: Introduced in ECMAScript 6, these are specialized data structures used for storing collections of data.
    let map = new Map();
    map.set('key', 'value');
    
    let set = new Set();
    set.add(1);
    

💡Note: Non-primitive data types (like objects and arrays) are mutable. You can modify the properties and elements of these objects directly. For example, if you have an object person with properties name and age, you can change the value of name or age directly without creating a new object.

javascript
data-types