JSFiddle - React, Tailwind, and code Playground

by Victor Algaze

HTML

Javascript Object Literals & Variable References

JavaScript

var myArray = ["item1", "item2", "item3"];

var myObject = {key1:"value1", key2:myArray}

//Array is updated, so object is updated
myArray.push("item4"); //myObject.key2 is updated with the item4
console.log(myObject.key2); //Object's array updated with new pushed value

//Object is updated, so array is updated
myObject.key2.push("item5"); //myArray is updated with the item5
console.log(myArray); //Array updated with item5



//If we clear out, however, the object retains the original values
myArray = ["muahahah, everything is wiped out"];
console.log("myArray", myArray); //returns ["muahahah, everything is wiped out"]
console.log("myObject.key2", myObject.key2); //Returns the original items 

//If we clear out the array via the object, the array does get updated
myObject.key2 = ["cleared the array from object"];
console.log("myArray", myArray); //returns ["cleared array"]
console.log("myObject.key2", myObject.key2); //returns ["cleared array"]