JSFiddle - React, Tailwind, and code Playground

by rufwork

JavaScript

var array = [];
var x = 4;
var y = {name: "test", type: "data", data: "2-27-2009"};

// pushes a copy
array.push(x);
x = 5;
document.write(array[0] + "<br>");    // alerts 4 because it's a copy

// pushes a reference
array.push(y);
y.name = "foo";
y = {};
y.name = 'bar';
document.write(array[1].name + ' :: ' + y.name + "<br>");   // alerts "foo :: bar" because it's a reference, but then the reference was moved.

array[1].name = 'foobar';
document.write(array[1].name + "<br>");   // alerts "foobar" because you used the array to point to the object that was initially in y.