130k views
5 votes
What is the output out of the following code? Explain your answer.

var a={},
b={key:'b'},
c={key:'c'};

a[b]=123;
a[c]=456;

(a[b]);

User Vzm
by
7.6k points

1 Answer

3 votes

Final answer:

The output of the code is 456. The variables a, b, and c are defined as objects with different key-value pairs. When assigning values to a[b] and a[c], the square brackets represent property names and a[b] is overwritten by a[c].

Step-by-step explanation:

The output of the code is 456.

In this code, the variables

a

,

b

, and

c

are defined as objects. They have different key-value pairs, with

b

having the value

'b'

and

c

having the value

'c'

.

When assigning values to

a[b]

and

a[c]

, the square brackets represent the property name of the object. Since

b

and

c

are both objects, they will be coerced to strings before being used as property names. As a result,

a[b]

and

a[c]

will both refer to the same property

[object Object]

in object

a

. Therefore, the value of

a[b]

is overwritten by

a[c]

to become

456

User LauroSkr
by
7.8k points

No related questions found