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

Practice this question and more.


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

  1. one two

  2. one two three four

  3. [one, two, three, four]

  4. ["one", "two", "three", "four"]

The correct answer is: one two three four

In the given code, the `array1` is initialized with the elements 'one' and 'two', while `array2` contains 'three' and 'four'. The key operation here is the `push` method, which is called with the spread operator (`...array2`). This operator expands the contents of `array2` and adds them as separate elements to `array1`. After executing `array1.push(...array2)`, `array1` will then contain four elements: 'one', 'two', 'three', and 'four'. When logging `...array1` to the console, the spread operator is again used, which takes each element of `array1` and outputs them as individual arguments. Therefore, the console output will display each element in `array1` separated by a space. Thus, the output of the code will be "one two three four", confirming that the selected answer is accurate.