LoginSignup
1
0

More than 1 year has passed since last update.

【Javascript】オブジェクト同士を比較できない理由 - オブジェクトの比較と参照

Last updated at Posted at 2023-05-23

オブジェクト同士を比較できない理由

.js
const cat = {
    type: 0
}

const chat = {
    type: 0
}

console.log(cat == chat); // false
console.log(cat.prop === chat.prop); // true

cat == chat)
cat, chatそれぞれに入っているのは、あくまでオブジェクトへの参照になる
参照を比較しているため、当然falseが返る

なので、cat.prop === chat.propのように、オブジェクトの中身で比較する必要がある

MEMO

プリミティブ型では、の比較を行い
オブジェクトでは参照の比較を行なっている

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0