You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript中为何[1] == [1]返回false?===也返回false吗?

Why does console.log([1] == [1]) return false in JavaScript?

Great question—this is a super common gotcha with reference types in JavaScript, so let's break it down step by step.

The core reason behind false

First off, arrays in JS are reference types, not primitive values (like numbers, strings, or booleans). Every time you write a literal like [1], you're creating an entirely new array object that lives in a unique spot in memory. Even if two arrays have identical content, they're separate entities with different memory addresses.

Both == (loose equality) and === (strict equality) treat reference types the same way here: they check if the two values point to the exact same object in memory, not whether their contents match. Since [1] and [1] are two distinct objects, both comparisons will return false.

Is there automatic type conversion happening with [1] == [1]?

Nope, no type conversion takes place here. The loose equality (==) rule only triggers type conversion when the two operands are of different types. When both are objects (arrays are technically objects under the hood), it skips conversion entirely and just checks for reference equality.

Does [1] === [1] also return false?

Absolutely. Strict equality (===) doesn't allow any type conversion at all, but that's not the issue here—we're still comparing two separate array objects. The only time === (or ==) will return true for arrays is if they reference the exact same object:

const arr1 = [1];
const arr2 = arr1; // arr2 now points to the same object as arr1
console.log(arr1 == arr2); // true
console.log(arr1 === arr2); // true

Quick side note: Comparing array contents

If you need to check if two arrays have identical content, you'll have to do it manually. For simple arrays of primitives, you could use something like JSON.stringify(arr1) === JSON.stringify(arr2) (though this has edge cases with values like undefined or functions). For more robust checks, write a custom function that iterates through each element and compares them one by one.

内容的提问来源于stack exchange,提问作者sinbar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:57:56