Skip to content

Instantly share code, notes, and snippets.

@machinefriendly
Last active December 1, 2016 21:19
Show Gist options
  • Select an option

  • Save machinefriendly/4f240e47c2f9cdd9fca42e7f68b046fd to your computer and use it in GitHub Desktop.

Select an option

Save machinefriendly/4f240e47c2f9cdd9fca42e7f68b046fd to your computer and use it in GitHub Desktop.

Revisions

  1. machinefriendly revised this gist Dec 1, 2016. 1 changed file with 16 additions and 2 deletions.
    18 changes: 16 additions & 2 deletions jscompareoperator.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@

    // == used to compare string, number, object (including array) >> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
    // 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:

    @@ -15,4 +16,17 @@ 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:

    // 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
    true
    d = {a:'b',c:'d'}
    Object {a: "b", c: "d"}
    c == d
    false
  2. machinefriendly created this gist Dec 1, 2016.
    18 changes: 18 additions & 0 deletions jscompareoperator.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@


    // 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: