Question 5
Back to Exercise
What does the following JavaScript code print (console output, as in Node.js)?
const a = [1, 2, 3, 4, 5];
console.log(a.splice(-2), a);
- A [ 1, 2, 3 ] [ 4, 5 ]
- B [ 4, 5 ] [ 1, 2, 3, 4, 5 ]
- C [ 3, 4, 5 ] [ 1, 2 ]
- D [ 4, 5 ] [ 1, 2, 3 ] Correct
Correct Answer: Option D
splice(-2) starts two from the end and, without a delete count, removes everything from there. It returns the removed items.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!