Skip to content

Primitive vs non-primitive data types in JavaScript

  • by

The main difference between Primitive and non-primitive data types is that Non-primitive data types are mutable, meaning that their values can be changed after they are created. And Primitive data types are immutable, meaning that their values cannot be changed once they are created.

Primitive data types are stored directly in the variable’s memory location, whereas non-primitive data types are stored as references to their memory locations.

Primitive data types in JavaScript include. It’s faster to access and manipulate than non-primitive data types.

  1. Number
  2. String
  3. Boolean
  4. Null
  5. Undefined
  6. Symbol
  7. BigInt (added in ECMAScript 2020)

Non-primitive (or object) data types in JavaScript include: Any changes made to the variable will affect the original value.

  1. Object
  2. Array
  3. Function
  4. Date
  5. RegExp
  6. Map
  7. Set
  8. and others

Primitive vs non-primitive data types in JavaScript examples

Simple example code.

<!DOCTYPE html>
<html>
<body>
<script>
    //primitive data types
    let name = 'John'; // string
    let age = 30; // number
    let salary = 50000n; // bigint
    let isStudent = false; // boolean
    let car = undefined; // undefined
    let home = null; // null

    console.log(name); 
    console.log(age); 
    console.log(salary); 
    console.log(isStudent); 
    console.log(car); 
    console.log(home);

    // non-primitive data types
    let person = {
        name: 'John',
        age: 30,
        isStudent: false
    }; // Object

    let fruits = ['apple', 'banana', 'orange']; // Array

    function sayHello(name) {
        console.log(`Hello ${name}!`);
    } // Function

    console.log(person);
    console.log(fruits);
    console.log(sayHello); 
</script>
</body>
</html>

Output:

Primitive vs non-primitive data types in JavaScript
Primitive data structureNon-primitive data structure
The primitive data structure is a kind of data structure that stores data of only one type.The non-primitive data structure is a type of data structure that can store data of more than one type.
Examples of primitive data structures are integer, character, and float.Examples of the non-primitive data structure are Array, Linked list, and stack.
The primitive data structure will contain some value, i.e., it cannot be NULL.The non-primitive data structure can consist of a NULL value.
The size depends on the type of data structure.In the case of non-primitive data structure, size is not fixed.
It starts with a lowercase character.It starts with an uppercase character.
The primitive data structure can be used to call the methods.The non-primitive data structure cannot be used to call the methods.

Comment if you have any doubts or suggestions on this JS datatype topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading