DEV Community

Akshay Mandliya
Akshay Mandliya

Posted on

Advance JS Quiz Series (Part-1)

1. What's the output?
let c = { greeting: 'Hey!' };
let d;

d = c;
c.greeting = 'Hello';
console.log(d.greeting);
Enter fullscreen mode Exit fullscreen mode
  • A: Hello
  • B: Hey!
  • C: undefined
  • D: ReferenceError
  • E: TypeError

Comment your Answer Below

Latest comments (6)

Collapse
 
hermannp profile image
hermann-p

As c is no primitive, d = c sets the value of d to the same reference pointer that references c.
That means c.greeting = ... means "lookup the object referenced by c and mutate its value".
As c and d 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 ;)

Collapse
 
studiospindle profile image
Remi Vledder

So this would be a example of "Pass by reference"?

Collapse
 
niewview profile image
NiewView

A. Hello

Collapse
 
akshaydatacode profile image
Akshay Mandliya

correct answer

Collapse
 
quanptit18 profile image
Bùi Hồng Quân

A: Hello

Collapse
 
akshaydatacode profile image
Akshay Mandliya

correct