Page 195 - JavaScript
P. 195
if (!Object.is) {
Object.is = function(x, y) {
// SameValue algorithm
if (x === y) { // Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
return x !== 0 || 1 / x === 1 / y;
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
}
};
}
Points to note
NaN itself is a number, meaning that it does not equal to the string "NaN", and most importantly
(though perhaps unintuitively):
typeof(NaN) === "number"; //true
Short-circuiting in boolean operators
The and-operator (&&) and the or-operator (||) employ short-circuiting to prevent unnecessary
work if the outcome of the operation does not change with the extra work.
In x && y, y will not be evaluated if x evaluates to false, because the whole expression is
guaranteed to be false.
In x || y, y will not be evaluated if x evaluated to true, because the whole expression is
guaranteed to be true.
Example with functions
Take the following two functions:
function T() { // True
console.log("T");
return true;
}
function F() { // False
console.log("F");
return false;
}
Example 1
T() && F(); // false
Output:
https://riptutorial.com/ 152

