【问题标题】:Why === and == giving false for following? [duplicate]为什么 === 和 == 给出错误的关注? [复制]
【发布时间】:2018-08-23 03:29:38
【问题描述】:

我知道问这个问题很愚蠢,但谁能告诉我 为什么 === 和 == 为跟随而给出错误。

x=[[1,2]];
console.log(x[0]===[1,2]);
console.log(x[0]==[1,2]);

这里 typeof(x[0]) 和 typeof([1,2]) 也是一样的,那为什么给假呢?

【问题讨论】:

标签: javascript


【解决方案1】:

因为它们在内存中是不同的值。

x=[[1,2]];
console.log(x[0]===[1,2]); // Here you're creating a new array in memory
console.log(x[0]==[1,2]); // Here you're creating a new array in memory

var y = x[0]; //Same value in memory

console.log(x[0]===y);
console.log(x[0]==y);

Equality comparisons and sameness

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2012-10-24
    相关资源
    最近更新 更多