Check If Two JavaScript Arrays Have the Same Values?

Abhinav Akhil
3 min readMar 2, 2023

--

In JavaScript, arrays are used to store multiple values in a single variable. Sometimes, you may need to check if two arrays have the same values. There are different ways to compare two arrays in JavaScript, but the most common way is to compare each element of the arrays.

To check if two JavaScript arrays have the same values, you can write a function that compares each element of the arrays. Here’s an example function that does this:

function arraysEqual(a, b) {
// Check if the arrays have the same length
if (a.length !== b.length) {
return false;
}

// Compare each element of the arrays
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}

// If all elements are equal, return true
return true;
}

This function takes two arrays a and b as arguments, and returns true if they have the same values and false otherwise. The function first checks if the arrays have the same length, and if they don't, it immediately returns false. Then, it loops through each element of the arrays and compares them. If it finds any elements that are not equal, it returns false. If it loops through all the elements without finding any differences, it returns true.

Here’s an example usage of the function:

const a = [1, 2, 3];
const b = [1, 2, 3];
const c = [1, 2, 4];

console.log(arraysEqual(a, b)); // Output: true
console.log(arraysEqual(a, c)); // Output: false

In this example, the arraysEqual() function is used to compare arrays a and b, which have the same values, and arrays a and c, which have different values.

We can also use the built-in Javascript functions to do the same, Let's check a few of them.

Use the every() method

The every() method helps you execute a function for each element of your array. This function is referred to as the callback function. It has access to some basic parameters like the element, index, and lots more, which we can use within the function:

function arraysEqual(a, b) {
return a.length === b.length && a.every((value, index) => value === b[index]);
}

This function uses the every() method to check if each element of array a is equal to the corresponding element of array b. The every() method returns true if all elements in the array pass the test implemented by the provided function.

Use the JSON.stringify()method

Another way to compare arrays in JavaScript is to convert them to strings using the JSON.stringify() method and then compare the strings. This method works only if the arrays contain primitive values and not objects or functions.

const a = [1, 2, 3];
const b = [1, 2, 3];
const c = [1, 2, 4];

console.log(JSON.stringify(a) === JSON.stringify(b)); // Output: true
console.log(JSON.stringify(a) === JSON.stringify(c)); // Output: false

In this example, the JSON.stringify() method is used to convert the arrays a, b, and c to strings, and then the strings are compared using the === operator.

Use the .toString()method

Similar to JSON.stringify(), this method converts any data type to a string and can similarly convert an object to a string.

function arraysEqual(a, b) {
return a.toString() === b.toString();
}

This function converts the arrays to strings using the toString() method and then compares the strings. This method works only if the arrays contain primitive values and not objects or functions.

Thanks for reading. Happy Coding!

--

--

Abhinav Akhil
Abhinav Akhil

Written by Abhinav Akhil

I create software, that tell stories.

Responses (1)