Skip to content

Instantly share code, notes, and snippets.

@tmcw
Last active December 13, 2015 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/4989663 to your computer and use it in GitHub Desktop.
Save tmcw/4989663 to your computer and use it in GitHub Desktop.
// the difference between == and ===
// double equals tests loose equality
5 == 5;
// it will convert types silently
'5' == 5;
// triple equals will not
'5' === 5;
// let's think about **objects**
// two objects with the same attributes are neither
// double or triple equal
({ foo: 'bar' } == { foo: 'bar' });
({ foo: 'bar' } === { foo: 'bar' });
// objects are only equal to exactly themselves
var myObject = { foo: 'bar' };
myObject === myObject;
// so in order to test object equality, we can either
// do a 'deepEquals' like underscore:
require('http://underscorejs.org/underscore-min.js');
_.isEqual({ foo: 'bar' }, { foo: 'bar' });
// or we can use a key function that returns something
// we **can** easily test the equality of
function key(obj) {
return obj.foo;
}
// and then it's easy to compare that value
key({ foo: 'bar' }) === key({ foo: 'bar' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment