What is the output of the following code when executed? const value = { number: 10 }; const multiply = (x = { ...value }) => { console.log((x.number *= 2)); }; multiply(); multiply(); multiply(value); multiply(value);

Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

To understand the output of the code, we need to examine how the function multiply and the spreading operator work in JavaScript, specifically how the default parameter and object mutability are interacting in this scenario.

Firstly, the value object is initialized with a property number set to 10. When the multiply function is called without arguments, it uses the spread operator to create a shallow copy of value. This means that on the first call of multiply(), a new object is created with a number property initialized to 10. Importantly, this new instance is not linked to the original value object, allowing it to be modified independently.

On the first call of multiply(), the new object's number property is multiplied by 2, resulting in 20. This value is then logged to the console. The second call of multiply() behaves the same way, again creating a new object with number set to 10, and after multiplying it by 2, it logs 20 again.

On the third call, multiply(value) is executed. Here, you are passing the original value object. In this instance, the number property of

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy