Check if Two Arrays Are Equal

easy
By FrontendAtlas Editorial · Updated Oct 10, 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.

Solution idea

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.

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