# Value-Based Comparison Operators for Arrays of Primitives in JavaScript

**URL:** <https://es.discourse.group/t/value-based-comparison-operators-for-arrays-of-primitives-in-javascript/2374>\
**Category:** 🦋 Proposals\
**Tags:** proposal\
**Created:** [May 22, 2025, 12:06pm UTC](https://es.discourse.group/t/value-based-comparison-operators-for-arrays-of-primitives-in-javascript/2374 "2025-05-22T12:06:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PriyanshuSinghR](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/priyanshusinghr/32/2470_2.png) [@PriyanshuSinghR](https://es.discourse.group/u/PriyanshuSinghR)\
**Post date:** [May 22, 2025, 12:06pm UTC](https://es.discourse.group/t/value-based-comparison-operators-for-arrays-of-primitives-in-javascript/2374/1 "2025-05-22T12:06:31Z")

</div>

#### Proposal: [https://github.com/tc39/proposal-array-value-comparison-operators](https://github.com/tc39/proposal-array-value-comparison-operators)

* * *

## Introduction

This proposal introduces value-based comparison behaviors for arrays of **primitive values** in JavaScript using either enhancements to the `==` operator or the introduction of new operators (`#==`, `&==`, `|==`).

Currently, JavaScript only supports **reference comparison** for arrays using both `==` and `===`, which can lead to unintuitive behavior in common use cases where developers expect arrays with the same primitive values to be considered equal.

* * *

## Problem Statement

JavaScript supports array destructuring, which is clean and expressive:

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

```

But checking that these variables match specific values becomes verbose:

```js
if (a === 1 && b === 2 && c === 3) {
  // do something
}

```

Trying the intuitive way fails:

```js
if ([a, b, c] == [1, 2, 3]) {
  // false — even though values are the same
}

```

This is because **`==` compares arrays by reference** , not by value.

## Why This Is a Problem

This behavior is inconsistent with how `==` works for primitives:

```js
2 == "2" // true 

```

But:

```js
[1, 2, 3] == [1, 2, 3] // false 

```

* * *

## Proposal Part 1: Improve `==` or Introduce `#==` for Array Value Comparison

### Option A: Make `==` Compare Arrays by Value (Only for Primitives)

Allow:

```js
if ([a, b, c] == [1, 2, 3]) {
  // true 
}

```

**Only when:**

- Both sides are arrays of the same length
- All elements are primitive values (number, string, boolean)

Keep `===` unchanged (still compares by reference).

* * *

### Option B: Add a New Operator: `#==`

If modifying `==` behavior is not preferred (due to backward compatibility concerns), introduce a new operator:

```js
if ([a, b, c] #== [1, 2, 3]) {
  // true 
}

```

This operator would **only compare arrays by value** — ideal for destructured values.

* * *

## Proposal Part 2: Add Logical Value Comparison Operators: `&==` and `|==`

To simplify more complex checks, introduce:

### `&==`: All elements match

Equivalent to:

```js
if (a === 1 && b === 2 && c === 3)

```

Syntax:

```js
if ([a, b, c] &== [1, 2, 3]) {
  // true only if all match
}

```

* * *

### `|==`: Any element matches

Equivalent to:

```js
if (a === 1 || b === 2 || c === 3)

```

Syntax:

```js
if ([a, b, c] |== [1, 2, 3]) {
  // true if any match
}

```

* * *

---

<div class="post-metadata">

**Author:** ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)\
**Post date:** [May 22, 2025, 3:33pm UTC](https://es.discourse.group/t/value-based-comparison-operators-for-arrays-of-primitives-in-javascript/2374/2 "2025-05-22T15:33:31Z")

</div>

We are extremely unlikely to add new syntax for this, and we definitely can't change the behavior of `==` without breaking everything.

But we might add a new function for doing element-wise comparison of arrays (there is [an old proposal](https://github.com/tc39/proposal-array-equality), though it hasn't had movement for a long time). This comes up fairly often; see e.g. [this conversation](https://matrixlogs.bakkot.com/TC39_General/2025-04-10#L4) or [this thread](https://es.discourse.group/t/array-prototype-equals/83/2).
