How to check Array Equality in JavaScript?

Let’s unfold the concept of array equality in JavaScript, a key aspect of the language that involves comparing two arrays for equality.

This article provides solutions on how to check array equality using various methods, which you’ll see below.

So, let’s get started to learn how these methods work with examples and understand the nuances of comparing objects in JavaScript.

What is array equality in JavaScript

Array equality in JavaScript, is a concept that involves comparing two arrays to check if they are equal.

This can be a bit tricky because arrays are objects in JavaScript, and comparing objects can have some nuances.

Determining if two arrays are equal is not as straightforward as it might seem.

Here’s a simplified explanation:

Strict Equality (===)

The strict Equality (===) operator checks if both arrays point to the exact same memory location, meaning they are the same instance.

It doesn’t compare the actual contents of the arrays.

For instance:

let samplearray1 = [10, 20, 30];
let samplearray2 = [10, 20, 30];
console.log(samplearray1 === samplearray1);  // true

console.log(samplearray1 === samplearray2); // false

In this case, samplearray1 and samplearray2 contain the same elements but are not the same instance, hence they are not strictly equal.

Abstract Equality (==)

The abstract Equality (==) operator attempts to convert the operands to a common type before comparison.

However, it’s generally advised to use === for clarity and to avoid unexpected behaviors.

Custom Function

Since JavaScript doesn’t provide a built-in method for comparing array contents, you can create a custom function to do this:

function areArraysEqual(samplearray1, samplearray2) {
if (samplearray1.length !== samplearray2.length) return false;

for (let i = 0; i < samplearray1.length; i++) {
if (samplearray1[i] !== samplearray2[i]) return false;
}

return true;
}

This function first checks if the arrays have the same length. If they do, it then compares each corresponding pair of elements.

Please keep in mind that these methods work well for arrays with primitive values (numbers, strings).

If the arrays contain objects or other arrays, you might need a more complex solution that performs a deep comparison.

How to check Array Equality in JavaScript?

In JavaScript, comparing arrays for equality can be a bit tricky because arrays are objects, and comparing objects doesn’t always yield the results you might expect.

Here’s a simple way to check if two arrays are equal:

function arraysAreEqual(array1, array2) {
// Check if the arrays are the same length
if (array1.length !== array2.length) {
return false;
}

// Check if all items exist and are in the same order
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false;
}
}

// Otherwise, return true
return true;
}

This function works by first checking if the two arrays are the same length. If they’re not, it immediately returns false.

If they are the same length, it then checks each item in the arrays to see if they’re equal.

If any pair of items is not equal, it returns false. If it makes it through both of these checks without returning false, it finally returns true, indicating that the arrays are equal.

Several ways to check array equality in JavaScript

There are several ways to check array equality in JavaScript. Here are a few methods:

Use the JSON.stringify() method

The JSON.stringify() method converts a JavaScript object or value to a JSON string. If the arrays are equal, their JSON string representation will also be equal.

// Function to check array equality using JSON.stringify()
function arraysEqual(samplearray1, samplearray2) {
    return JSON.stringify(samplearray1) === JSON.stringify(samplearray2); ✅
}

// Test arrays
let samplearray1 = [10, 20, 30, 40, 50];
let samplearray2 = [10, 20, 30, 40, 50];
let samplearray3 = [10, 20, 30, 40, 90];

// Using arraysEqual function
console.log(arraysEqual(samplearray1, samplearray2));
console.log(arraysEqual(samplearray1, samplearray3)); 

Output:

true

false

Use the every() method

The every() method tests whether all elements in the array pass the test implemented by the provided function.

// Function to check array equality using every() method
function arraysEqual(samplearray1, samplearray2) {
    return samplearray1.length === samplearray2.length && samplearray1.every((value, index) => value ===  samplearray2[index]);✅
}

// Test arrays
let samplearray1 = [10, 20, 30, 40, 50];
let samplearray2 = [10, 20, 30, 40, 50];
let samplearray3 = [10, 30, 60, 80, 100];

// Using arraysEqual function
console.log(arraysEqual(samplearray1, samplearray2)); 
console.log(arraysEqual(samplearray1, samplearray3));

Output:

true
false

Use a simple for loop

You can also use a simple for loop to iterate over the arrays and compare each element.

// Function to check array equality using a for loop
function arraysEqual(samplearray1, samplearray2) {
    if (samplearray1.length !== samplearray2.length) return false;
    for (let i = 0; i < samplearray1.length; i++) { ✅
        if (samplearray1[i] !== samplearray2[i]) return false;
    }
    return true;
}

// Test arrays
let samplearray1 = [10, 20, 30, 40, 50];
let samplearray2 = [10, 20, 30, 40, 50];
let samplearray3 = [20, 40, 60, 80, 100];

// Using arraysEqual function
console.log(arraysEqual(samplearray1, samplearray2)); 
console.log(arraysEqual(samplearray1, samplearray3)); 

true
false

Conclusion

In conclusion, array equality in JavaScript is a nuanced concept due to the nature of arrays as objects.

While JavaScript does not provide a built-in method for comparing array contents, several techniques can be used to achieve this, including the JSON.stringify() method, the every() method, and a simple for loop.

These methods work well for arrays with primitive values. However, if the arrays contain objects or other arrays, a more complex solution that performs a deep comparison may be required.

We hope this article has provided you with enough information to understand the array equality JavaScript.

If you want to explore more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment