What will the console output for the code: let array1 = ['one', 'two']; let array2 = ['three', 'four']; array1.push(...array2); console.log(...array1);?

Prepare for the Salesforce JavaScript Developer I Certification Exam with quizzes, flashcards, and detailed explanations. Enhance your understanding and boost your confidence to ace your certification!

Multiple Choice

What will the console output for the code: let array1 = ['one', 'two']; let array2 = ['three', 'four']; array1.push(...array2); console.log(...array1);?

Explanation:
The console output will be "one" "two" "three" "four" due to the way the `push` method and the spread operator are used in the code. Here’s how it works: 1. An initial array, `array1`, is defined with the elements 'one' and 'two'. 2. Another array, `array2`, is defined with the elements 'three' and 'four'. 3. The `push` method is then called on `array1`, leveraging the spread operator `...array2`. This operator takes all the elements from `array2` and adds them individually to `array1`. 4. After the `push` operation, `array1` now contains four elements: 'one', 'two', 'three', and 'four'. 5. Finally, the `console.log(...array1)` prints each element of `array1` separately in the console. Therefore, since all elements of `array1` are outputted due to the spread syntax in `console.log`, the complete output will indeed be "one" "two" "three" "four".

The console output will be "one" "two" "three" "four" due to the way the push method and the spread operator are used in the code.

Here’s how it works:

  1. An initial array, array1, is defined with the elements 'one' and 'two'.

  2. Another array, array2, is defined with the elements 'three' and 'four'.

  3. The push method is then called on array1, leveraging the spread operator ...array2. This operator takes all the elements from array2 and adds them individually to array1.

  4. After the push operation, array1 now contains four elements: 'one', 'two', 'three', and 'four'.

  5. Finally, the console.log(...array1) prints each element of array1 separately in the console.

Therefore, since all elements of array1 are outputted due to the spread syntax in console.log, the complete output will indeed be "one" "two" "three" "four".

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy