+ 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);
15 Réponses
+ 5
The answer to this is 46
+ 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
+ 1
46
+ 1
46
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));
0
12
0
46
0
The correct answer is 46
- 1
46
- 1
46
- 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
- 1
46
- 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
- 1
46
- 1
The answer is not 46