Javatpoint Logo
Javatpoint Logo

How to check two array values are equal in JavaScript

Introduction:

Take into account that you have two Javascript arrays, each with a number of elements. You want to compare the arrays right now. In order to compare two arrays, you must determine whether they have the same number of elements and whether all of those elements have the same value. We say that two arrays are equal if they meet both of these requirements. Yet it's not as easy as it seems to compare two arrays. Let's investigate the various options.

Equality Comparison:

Javascript compares objects and arrays using references rather than values. It indicates that Javascript checks if the two arrays point to the same reference rather than comparing the values present in them, which is typically not the case even if the two arrays contain the same components. It means that using the tight or loose equality operators (i.e., == or ===) to compare two arrays in JavaScript will typically produce a false result. Let's use an illustration to try to better grasp this:

Example:

Output:

The arrays have different elements.

Even though both arrays' values are the same, as you can see in the example above, the comparison yields a false result. But, you can use this function to see if two pointers to the same element in an object or an array. Let's use an illustration to try to better grasp this:

Output:

The arrays have the same elements.

Array comparision Using JSON.stringify():

JSON.stringify () is another approach that is frequently used in JavaScript to compare two arrays. Javascript's JSON.stringify method transforms an object or array into a JSON string. This function allows us to serialise each array, after which we can compare the two serialised strings. Let's attempt to comprehend how this method might be put into practise:

Example:

Output:

The arrays have the same elements.

To demonstrate this point, consider the following example. However, there are some edge circumstances where this solution fails.

Output:

The arrays have the same elements.

In this example, the arrays are not equal, but it is proved that they are. The arrays' respective first items' values vary amongst them. Yet when transforming the array into a JSON string, the JSON.stringify() method does not take into account undefined or null values. As a result, it simply leaves them out, which is how the strings end up being equal. Such situations could appear extremely unusual, but they could make problems more difficult to resolve.

Array comparisionusing the Array.every() method:

The Array.every() method tests whether all elements in an array pass the test implemented by the provided function. If the function returns true for all elements, then every() returns true. Otherwise, it returns false.

Here's an example that checks if two arrays have equal values:

Example:

Output:

The arrays have equal values.

In this example, the every() method is used to check if all values in array1 are equal to the corresponding values in array2. If any value is not equal, the every() method will return false and the arrays will be considered to have different values.

Conclusion:

In Javascript, comparing two arrays entails determining if each array has the same number of elements and whether all of those elements have the same value.

These methods are like;

  • Using the operators == or === for equality comparisons.
  • Arrays are converted to JSON strings using JSON.stringify(), which is followed by comparison.
  • Array comparision using the Array.every() method.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA