厳密等価比較とSameValueとSameValueZeroの違い
JavaScript、値が同一であるか比較するメソッドが抽象比較・厳密等価比較・SameValue・SameValueZeroの4つあるのこの世の終わり
抽象比較……==
厳密等価……===
SameValue……Object.is()
SameValue……Array.includes()
それぞれのアルゴリズムの処理は以下を参照
厳密等価は「等価比較アルゴリズム」、SameValue(と同Zero)は「同値比較アルゴリズム」として作られている。
違い
+0 と -0
code:script.js
equivalenceCheck(
(a, b) => a == b, // 抽象等価比較アルゴリズム
(a, b) => a === b, // 厳密等価比較アルゴリズム
(a, b) => Object.is(a, b), // SameValueアルゴリズム
(a, b) => a.includes(b), // SameValueZeroアルゴリズム ); // Return => true true false true ×
code:script.js
equivalenceCheck(
(a, b) => a == b, // 抽象等価比較アルゴリズム
(a, b) => a === b, // 厳密等価比較アルゴリズム
(a, b) => Object.is(a, b), // SameValueアルゴリズム
(a, b) => a.includes(b), // SameValueZeroアルゴリズム ); // Return => false false true true ×