9.12 SameValue

2010-05-04

Boolean SameValue(x:mixed, y:mixed)

Shallow strict comparison of x and y.

Code: (Meta Ecma)
function SameValue(x,y){
if ((typeof x) != (typeof y)) return false;
if (typeof x == "undefined") return true;
if (typeof x == "null") return true;
if (typeof x == "number") {
if (isNaN(x)) return true; // note, this is different from NaN==NaN !
// zero's aren't always the same
if (x === +0 && y === -0) return false;
if (x === -0 && y === +0) return false;
// the next step is: "if x is the same Number value as y, return true. otherwise return false."
return (x == y);
}
// for strings the result is whether they are the same string. for objects they should
// point to the same reference. Both are checked through the identity operator ===
return x === y;
}

Note that the only oddity is the fact that NaN can be equal to NaN, which is not the case in the Ecmascript environment.