JavaScript - Reference Type
JavaScript - Reference Type
JavaScript Reference Type
There are two types of data types in JavaScript: primitive and reference type.
Primitive data types are immutable, which means that they cannot be changed. The primitive data types in JavaScript are: Number, String, Boolean, Undefined, Null, Symbol.
Reference data types are mutable, which means that they can be changed. The reference data types in JavaScript are: Object, Array, Function.
When you assign a primitive data type to a variable, the variable gets a copy of the value. When you assign a reference data type to a variable, the variable gets a reference to the value. This means that if you change the value of a reference data type, the change will be reflected in all of the variables that reference that value.
For example, the following code creates two variables, x and y, and assigns them the value 10:
let x = 10;
let y = 10;The variables x and y both have a copy of the value 10. If you change the value of x, the value of y will not change.
x = 20;
console.log(x); // 20
console.log(y); // 10The following code creates two variables, x and y, and assigns them an array:
const x = [1, 2, 3];
const y = x;The variables x and y both reference the same array. If you change the value of the array that x references, the change will be reflected in the array that y references.
x[0] = 4;
console.log(x); // [4, 2, 3]
console.log(y); // [4, 2, 3]It is important to understand the difference between primitive and reference data types in JavaScript so that you can write code that is efficient and predictable.
Objects and functions are the two main reference types in JavaScript and are explained as follows.
Object
Objects are unordered collections of key-value pairs, where keys are strings or symbols, and values can be any data type, including other objects.
const person = {
firstName: "John",
lastName: "Doe",
age: 30
};Function
Functions are also reference types in JavaScript. Functions are special types of objects that can be invoked (called) to perform a task.
function greet(name) {
alert("Hello, " + name + "!");
}Examples
Example 1: Object Mutability
In this example, we demonstrate object mutability for which first an object is created and then modifications are made through that reference which in turn affect the original object. The person object is modified via the reference from the anotherPerson precisely the age which is changed from 25 to 30. As seen in the output, the original object changes after the modification hence the object is said to be mutated.
Example 2: Array Modification
Arrays which can store multiple values of different data types in JavaScript inside of a single variable are demonstrated here. They exhibit mutability which means then when a reference is made to an array, changes made to the reference also reflect in the original array. Here we create an array of colors and modify it through the reference of moreColors, mainly by pushing an element yellow.
Example 3: Function Reference Type
In this example, we create a function greet whose reference is initially assigned to greetingFunction. After using it to say Hello, we modify the reference to point to a different function which greets with Hola. This demonstrated the flexibility of function references in JavaScript.
Example 4: Custom class
This example demonstrated custom classes in JavaScript another crucial aspect from the point of view reference types. The class consists of properties and functions/methods. Here we create a class Book with a constructor and a method. 4 instances of this book class are created namely (book1, book2, book3, book4) and are given the respective data such as title, author and genre.
Example 5: Immutable Objects
This example focuses on immutability of objects which is achieved using the Object.freeze() while creating the object. Immutability basically means the object will not change after creation or simply ensures that once an object is defined, its properties cannot be modified. Here we create a person object with name & age as properties and then attempt to change the age to 35. However, this modification is prevented by the frozen state of the object and throws an error. Immutability is an important aspect as helps maintain data integrity thereby preventing unintended changes and enhancing the predictability in code execution.