Setting one object equal to another object with the assignment operator in Javascript

Whenever I need to copy one object to another in JS, I just cast it to a primitive:

var newObject = JSON.stringify(oldObject);

Then when I need to use it:

var evenNewerObj = JSON.parse(newObject);

Hope this helps someone.


In JavaScript, primitive types are copied by value and reference types are copied by reference. More info here: http://docstore.mik.ua/orelly/web/jscript/ch09_03.html


It equals 8.

pnt2 = pnt1

That statement is pointing the pnt2 object to the pnt1 object so any modification you do to pnt1 will show up in pnt2.