12/1/2016 - 9:13 PM


// == used to compare string, number, object (including array)  >>
// object should be same reference (in same memory) !!!

// Arrays are Objects in JavaScript which are pass by reference. This means that when I initialize an array:

var array = [1, 2, 3];
// I've created a reference to that array in memory. If I then say:

var otherArray = [1 2, 3];
// array and otherArray have two separate addresses in memory so they will not equal eachother (even though the values are equal.) To test out the pass by reference you can play around with arrays by doing:

var copyArray = array;
// In this case, copyArray is referencing the same address in memory as array so:

copyArray === array; //will evaluate to true
array.push(4); //copyArray will now have values [1, 2, 3, 4];
copyArray.push(5); //array will have values [1, 2, 3, 4, 5];
copyArray === array; //still true

// In order to test equality of values in an array or object you need to do a 'deep comparison' - for Arrays this will traverse both arrays to compare index and value to see if both are equal - for objects it will traverse every key and makes sure the value is the same. For more on deep comparisons you can check out the underscore.js annotated source:

// object example:
a = {a:'b',c:'d'}
Object {a: "b", c: "d"}
c = a
Object {a: "b", c: "d"}
c == a
d = {a:'b',c:'d'}
Object {a: "b", c: "d"}
c == d