PROGRAMMING

 
REMEMBERS




Last update:   29-10-2021

Primitives vs. Objects

One of the fundamental differences of objects vs primitives. Objects are stored and copied by reference.

Primitives

We have two independent variables, each one is storing the string "Hello!".
 
let message = "Hello!";

let phrase = message;

Objects

With objects we have two variables, each one with the reference to the same object. Here, the object is stored somewhere in memory, and the user has a “reference” to it. When an object variable is copied – the reference is copied.
 
let user = { name: 'John' };

let admin = user;

admin.name = 'Pete'; 

console.log(user.name);  // Pete - from the user reference
Not Equal    (2/3)

Not equal

Here two independent objects are not equal, even though both are empty.
 
let a = {};
let b = {}; // two independent objects

alert( a == b ); // false
Task

Make a duplicate of the user (no reference). Hint: copy user properties
 
let user = {
  name: "John",
  age: 30
};

let clone = {}; // new empty object

// your code here



// your code END

clone.name = "Pete"; // changed the data

console.log(user.name); // John - from the original object

Questions    
Methods

        A B C D E F
🔔
1/1