1. What's the output?
let c = { greeting: 'Hey!' };
let d;
d = c;
c.greeting = 'Hello';
console.log(d.greeting);
- A:
Hello
- B:
Hey!
- C:
undefined
- D:
ReferenceError
- E:
TypeError
For further actions, you may consider blocking this person and/or reporting abuse
Remi -
Sandy Galabada -
Safdar Ali -
Ini Frank -
Top comments (6)
As
c
is no primitive,d = c
sets the value ofd
to the same reference pointer that referencesc
.That means
c.greeting = ...
means "lookup the object referenced by c and mutate its value".As
c
andd
are only references to the same object, the Output is, of course,'Hello'
.Everyone confused by this behavior should start looking into the basics of a functional programming style ;)
A: Hello
correct
A. Hello
correct answer
So this would be a example of "Pass by reference"?