-
-
Save lc512k/1a28ea35b6a4f4de6ed3 to your computer and use it in GitHub Desktop.
Value equality with coercion. Edge cases.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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