LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 5

splice with a negative start Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links