Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Value equality with coercion. Edge cases.
var testStr = function () {
var a = "5";
var b = "44";
console.log("num str", a > b); // true, lex comparison
};
testStr();
var testNaN = function () {
var a = 6;
var b = "five"; // coerced to NaN, neither lesser non greater
console.log("6 > five", a > b);
console.log("6 < five", a < b);
console.log("6 == five", a == b);
};
testNaN();
var testArray = function () {
var a = [1];
var b = [1];
console.log('arrays', a == b); // false, obj references
};
testArray();
var testEmpty = function () {
var a = 0;
var b = []; //""
console.log('empty things', a == b); // true, both coerced to num when either is num
};
testEmpty();
var testBool = function () {
var a = "";
var b = false;
console.log('bools', a == b); // true, both coerced to bool when both bool
};
testBool();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment