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 be the output of the following code: const arr = [1, 2, 3]; arr.push(6); console.log(arr); arr.pop(); console.log(arr); arr.shift(); console.log(arr); arr.unshift(8); console.log(arr);?

  1. [1, 2, 3, 6]

  2. [2, 3, 6]

  3. [8, 2, 3]

  4. [8, 1, 2, 3]

The correct answer is: [2, 3, 6]

The code provided performs a series of array manipulations using methods that modify the original array. Initially, the array `arr` is declared and initialized with the elements `[1, 2, 3]`. When the `push(6)` method is called, the value `6` is added to the end of the array, resulting in `[1, 2, 3, 6]`. This is confirmed by the first `console.log(arr)`, which outputs the updated state of the array. Next, the `pop()` method removes the last element from the array. Since the last element is `6`, after this operation, the array reverts to `[1, 2, 3]`. This is echoed in the next `console.log(arr)` statement. Subsequently, `shift()` is called, which removes the first element of the array. Now, `1` is removed, leading to the array becoming `[2, 3]`, which is displayed with the next `console.log(arr)`. Finally, the `unshift(8)` method adds `8` to the front of the array, resulting in the array transforming to `[8, 2, 3]`. This last change