minte9
LearnRemember



Reference

A variable assigned to an object stores a reference to it.
 
/**
 * Variable, alwayas copied as value
 * Objects, stored by reference
 * 
 * When we copy an object, the resulted two variables ...
 * are references to tht same object
 */

let a = "Hello World";
let A = {
    'name': 'John',
    'age': 30,
}

let x = a; // copy by value
let B = A; // copy by reference

B.name = 'Sam'; // changes reference object

console.log(A === B);           // true
console.log(A.name == 'Sam');   // true (copy by reference)
console.log(A.name != 'John');  // true

Equality

Two objects are equal only if they are the same object.
 
/**
 * Two independent objects are not equal, 
 * even thought they look alike
 */

let A = {};
let B = {};
let C = B;

console.log(A == B);  // false (different objects)
console.log(C === B); // true (reference to the same object)



  Last update: 303 days ago