FrontendAtlas
Interview Questions
Home>JavaScript interview questions> JavaScript coding challenges >Check if Two Arrays Are Equal

Check if Two Arrays Are Equal

easy
By FrontendAtlas Editorial · Updated Jan 30, 2026
Check whether two arrays are equal by length and by value at each index. This is a shallow equality check for primitive elements: order matters, and any mismatch should return false immediately. It highlights iteration, early exits, and strict equality behavior.

Arguments

  • a: any[] — The first array to compare.
  • b: any[] — The second array to compare.

Returns

boolean — true if both arrays are identical in length and elements (in the same order), otherwise false.
Examples
// Basic examples
arraysEqual([1, 2, 3], [1, 2, 3]) // => true

arraysEqual([1, 2, 3], [3, 2, 1]) // => false

// Different lengths
arraysEqual([1, 2], [1, 2, 3]) // => false

// Works with strings
arraysEqual(['a', 'b'], ['a', 'b']) // => true

// Works with mixed types
arraysEqual([1, '2', 3], [1, 2, 3]) // => false

Solution

Overview

Compare arrays by first checking length and then scanning index-by-index for strict equality. This is the standard shallow equality test when arrays contain primitives or stable references, and it avoids expensive conversions or nested loops.

1

Approach 1: Loop-based comparison (explicit and efficient)

Algorithm:

1) If a === b, return true (same reference).

2) If either is not an array or lengths differ, return false.

3) Loop through indices; if a[i] !== b[i], return false.

4) If the loop completes, return true.

Complexity: O(n) time, O(1) space. This intentionally does not deep-compare nested objects.

export default function arraysEqual(a, b) {
  if (a.length !== b.length) return false;
  for (let i = 0; i < a.length; i++) {
    if (a[i] !== b[i]) return false;
  }
  return true;
}
export default function arraysEqual(a: any[], b: any[]): boolean {
  if (a.length !== b.length) return false;
  for (let i = 0; i < a.length; i++) {
    if (a[i] !== b[i]) return false;
  }
  return true;
}
2

Approach 2: Using every() (declarative style)

Idea: use Array.prototype.every() to ensure that every element in a matches the element at the same index in b.

How it works:

  1. If a.length !== b.length, return false.
  2. Otherwise, return a.every((el, i) => el === b[i]).

Why use it: more declarative and concise than a manual loop, while still running in O(n) time and short-circuiting on the first mismatch.

export default function arraysEqual(a, b) {
  return a.length === b.length && a.every((el, i) => el === b[i]);
}
export default function arraysEqual(a: any[], b: any[]): boolean {
  return a.length === b.length && a.every((el, i) => el === b[i]);
}
3

Approach 3: JSON.stringify shortcut (not recommended for complex data)

Idea: serialize both arrays to JSON strings and compare the resulting strings.

How it works:

  1. Compute JSON.stringify(a) and JSON.stringify(b).
  2. Compare the two strings with === and return the result.

Why use it: very compact and can work for simple arrays of primitives, but it has important caveats — it does not handle undefined, functions, or differences in object key order reliably, so it is not suitable for robust deep equality checks.

export default function arraysEqual(a, b) {
  return JSON.stringify(a) === JSON.stringify(b);
}
export default function arraysEqual(a: any[], b: any[]): boolean {
  return JSON.stringify(a) === JSON.stringify(b);
}

Notes & Pitfalls

Pitfalls
  • Strict equality (`===`) does not perform deep comparison for objects or nested arrays; it compares references.
  • Avoid relying on `JSON.stringify` for nested structures, objects with varying key order, or values like `undefined`, functions, or symbols.
  • Be aware that `['1']` and `[1]` are not considered equal due to type differences.
Edge cases
  • Empty arrays → `true`.
  • Different lengths → `false`.
  • Different order → `false`.
  • Mixed types → `false` when any pair differs by value or type.
  • Nested arrays or objects → compared by reference in Approaches 1 and 2; not reliably by structure in Approach 3.
Techniques
  • Using index-based loops for element-wise comparison.
  • Using `every()` for a declarative comparison with early exit.
  • Understanding reference vs. value equality in JavaScript.

Common mistakes on this challenge

  • Array equality contract should be strict and index-based

    Equality here means same length and same value at each index with strict comparison.

    • Return false early when lengths differ.
    • Compare each index with strict equality (`!==`).
    • Do not sort arrays before comparison; order matters.

Resources

  • MDN – Array.prototype.every()
  • MDN – Equality comparisons and sameness

Similar questions

Remove DuplicateseasyFlatten One LeveleasyCapitalize Wordseasy

Guides

Frontend interview preparation guideGuideFrontend coding interview questions and prep guideBlueprintJavaScript Problems That Actually Show UpBlueprintBuild Great UI in 60 MinutesBlueprint

Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.

Open frontend interview questionsBrowse JavaScript interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen JavaScript mastery study planOpen Company PrepOpen JavaScript Framework Prep Guide
↗Incidents hub
40 / 88Next →→