Question in code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Question in code

i have the following code, when assigning the value to the "obj2" I have added new propeties and also have change the value of one that is already created. By saying c: 5 in the brackets, didnt i have change the value of c? because i believe that that piece of code is doing that, however c remains with the original value of 4, can someone break it down for me? const obj1 = { a: 0, b: 2, c: 4 }; const obj2 = Object.assign({c: 5, d: 6}, obj1); console.log(obj2.c, obj2.d);

10th May 2019, 5:22 PM
Elvin Borges
Elvin Borges - avatar
15 Answers
+ 5
The answer to this is 46
25th Jul 2020, 4:00 PM
Ekundayo Timileyin
+ 1
Elvin Borges obj2 is not assigned a value till rhs expression is evaluated {c:5, d:6} is a new object whose contents are overwritten with the contents of obj1. Since the new object has a key "c" in it, it's value changes to 4 after assignment. This new object is then assigned to obj2, and you are printing the values of keys "c" and "d" so you get 46 as answer
10th Feb 2020, 8:29 AM
Pranjal Singh
Pranjal Singh - avatar
+ 1
46
30th Jul 2020, 8:32 AM
kedir.y
kedir.y - avatar
+ 1
46
20th Aug 2020, 6:02 AM
Nadeesha Pathiraja
Nadeesha Pathiraja - avatar
0
What is the output of the following code? function magic(...nums) { let sum = 0; nums.filter(n => n % 2 == 0).map(el => sum+= el); return sum; } console.log(magic(1, 2, 3, 4, 5, 6));
12th May 2020, 7:03 AM
Abhitesh Singh
Abhitesh Singh - avatar
0
12
24th Nov 2020, 4:49 AM
Shaira Mae Gozun Corpuz
Shaira Mae Gozun Corpuz - avatar
0
46
3rd Dec 2020, 7:15 PM
El Maslohi Hassan
0
The correct answer is 46
22nd Jan 2022, 1:13 PM
James Obinna
- 1
46
23rd May 2019, 1:52 PM
Naveen kumar.T
Naveen kumar.T - avatar
- 1
46
4th Jul 2019, 5:24 AM
Eric Munene
Eric Munene - avatar
- 1
What is the output of this code? const obj1 = { a: 0, b: 2, c: 4 }; const obj2 = Object.assign({c: 5, d: 6}, obj1); console.log(obj2.c, obj2.d); Answer is:46
5th Aug 2019, 2:07 PM
Aftab Ashraf
Aftab Ashraf - avatar
- 1
46
16th Sep 2019, 11:20 AM
ISINGIZWE GASANA Aline
ISINGIZWE GASANA Aline - avatar
- 1
What is the output of this code? const obj1 = { a: 0, b: 2, c: 4 }; const obj2 = Object.assign({c: 5, d: 6}, obj1); console.log(obj2.c, obj2.d); answer 46
24th Dec 2019, 9:23 AM
Rana Abid
Rana Abid - avatar
- 1
46
11th May 2020, 8:51 PM
Zainab Malkawi
Zainab Malkawi - avatar
- 1
The answer is not 46
28th Jun 2020, 9:13 AM
Faith Mundi
Faith Mundi - avatar