How to Compare Arrays in JavaScript?

In JavaScript, there are various ways to compare arrays. The Array prototype provides several methods for comparing arrays. Libraries and functions also enable observing differences between arrays.

After reading this article, you will be able to use different methods for comparing arrays in JavaScript, including native methods and external libraries, and much more!

How to compare arrays in JavaScript

Let's explore the different ways to compare arrays in JavaScript without further delay.

How to compare arrays in JavaScript

The solutions presented below cover various comparison scenarios, including array intersections and differences.


If you know that you'll be dealing with large arrays, it may be necessary to fine-tune the solutions to improve performance.


How to check if two arrays are identical?

Here's one way to check if two arrays are identical. This means that both arrays have the same values at the same positions.


let array1 = ["a", "b", "c", "d"];
let array2 = ["a", "b", "c", "d"];

function isEqual(array1, array2) {
  if (array1.length !== array2.length) return false;

  return array1.every((value, index) => value === array2[index]);
}

let result = isEqual(array1, array2);

console.log(result);

// true

In this solution, we created a function to make our comparison code easily reusable.

The first thing we check is if the two arrays have different lengths.

This is a simple and important check because there is no need to iterate over the arrays if we already know they are different.

Then, we use the `every` prototype method to compare each element of the two arrays.

The `every` function performs a test on each element of the array it is used on.

In this case, we use the `index` variable to compare the values of the two arrays.


Comparing arrays with a library like LoDash

If you find yourself writing a lot of functions to work with arrays and objects, you might consider using a library.

LoDash is a great option and has a built-in function to check if two arrays are equal.


After including the LoDash library, you can use the `isEqual` function.

let array1 = ["a", "b", "c", "d"];
let array2 = ["a", "b", "c", "d"];

let result = _.isEqual(array1, array2);

console.log(result);

// true


Using JSON.stringify to compare arrays

One very simple way to compare two arrays is to convert them into strings using JSON.stringify, and then compare the two strings.

This solution makes our code very simple but is not particularly performant.

We may also encounter issues when the string representation of certain values does not match, leading to hard-to-debug errors.

let array1 = ["a", "b", "c", "d"];
let array2 = ["a", "b", "c", "d"];

function isEqual(array1, array2) {
  if (array1.length !== array2.length) return false;

  return JSON.stringify(array1) === JSON.stringify(array2);
}

let result = isEqual(array1, array2);

console.log(result);

// true

How to check if two arrays have the same values?

Here's how to check if two arrays have the same values, but not necessarily in the same order.

let array1 = ["a", "b", "c", "d"]
let array2 = ["d", "b", "a", "c"]

function isEqual(array1, array2) {
  if (array1.length !== array2.length) return false

  let matching = array1.filter(val => array2.includes(val))

  if (matching.length == array1.length) return true

  return false
}

let result = isEqual(array1, array2)

console.log(result)

// true

Once again, we first check if the two arrays have the same length to save computation time.

Then, we perform a filtering operation, where we use the `includes` prototype to check if the second array has each value at a certain index.

This filtering operation will return an array of matches, so if the length is the same as our input array, we know that our compared arrays contain the same values.


How to find the corresponding values between two arrays (intersection)? 

Here's how you can obtain the intersection of arrays. An intersection is simply a comparison of two arrays that returns the values present in both arrays.

let array1 = ["a", "b", "c", "d"]
let array2 = ["a", "b", "3", "4"]

let intersection = array1.filter(value => array2.includes(value))

console.log(intersection)

// ["a", "b"]

How to find the values that are not in both arrays (difference)? 

Here's how you can find the difference between two arrays. The difference is the inverse of the intersection. It represents all the values that are not shared between the two arrays.

let array1 = ["a", "b", "c", "d", "e"]
let array2 = ["a", "b", "3", "4"]

let diff1 = array1.filter(val => !array2.includes(val))
let diff2 = array2.filter(val => !array1.includes(val))

let diffFinale = diff1.concat(diff2)

console.log(diffFinale)

// ["c", "d", "e", "3", "4"]


Next Post Previous Post