Skip to content

Instantly share code, notes, and snippets.

@guileen
Created May 11, 2011 05:53
Show Gist options
  • Save guileen/965989 to your computer and use it in GitHub Desktop.
Save guileen/965989 to your computer and use it in GitHub Desktop.
How javascript equals works
var console = console || require('console');
function foo(n){
this.name = n;
}
foo.prototype.toString = function(){ return this.name };
console.log(new foo('key') === 'key'); // false
console.log(null === undefined); // false
console.log(new foo('key') == 'key'); // true
console.log(null == undefined); // true
console.log(new foo('10') == 10); // true
console.log(new foo('10') < 11); // true
console.log(new foo('10') > 9); // true
console.log(new foo('1') == true); // true
console.log(new foo('0') == false); // true
console.log(!!(new foo('0'))); // true
map = {};
map['key'] = 'value';
console.log(map[new foo('key')] === 'value'); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment