in JavaScript by
Q:
Guess the outputs of the following code:

// Code 1:

let x= {}, y = {name:"Ronny"},z = {name:"John"};

x[y] = {name:"Vivek"};

x[z] = {name:"Akki"};

console.log(x[y]);

// Code 2:

function runFunc(){

  console.log("1" + 1);

  console.log("A" - 1);

  console.log(2 + "-2" + "2");

  console.log("Hello" - "World" + 78);

  console.log("Hello"+ "78");

}

runFunc();

// Code 3:

let a = 0;

let b = false;

console.log((a == b));

console.log((a === b));

1 Answer

0 votes
by
Code 1 - Output will be {name: “Akki”}.

Adding objects as properties of another object should be done carefully.

Writing x[y] = {name:”Vivek”} , is same as writing x[‘object Object’] = {name:”Vivek”} ,

While setting a property of an object, javascript coerces the parameter into a string.

Therefore, since y is an object, it will be converted to ‘object Object’.

Both x[y] and x[z] are referencing the same property.

Code 2 - Outputs in the following order:

11

Nan

2-22

NaN

Hello78

Code 3 - Output in the following order due to equality coercion:

true

false
Click here to read more about JavaScript
Click here to read more about Insurance

Related questions

0 votes
asked Feb 6, 2020 in JavaScript by rajeshsharma
0 votes
asked Feb 6, 2020 in JavaScript by rajeshsharma
+1 vote
asked Dec 4, 2019 in JavaScript by SakshiSharma
0 votes
asked Jan 18 in Python by SakshiSharma
0 votes
asked Jan 18 in Python by SakshiSharma
...