LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 6

splice that only inserts Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

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

main.js
const steps = ["start", "finish"];
const removed = steps.splice(1, 0, "build", "test");
console.log(removed, steps);
  • A [ 'finish' ] [ 'start', 'build', 'test' ]
  • B [] [ 'start', 'build', 'test', 'finish' ] Correct
  • C [] [ 'build', 'test', 'start', 'finish' ]
  • D undefined [ 'start', 'build', 'test', 'finish' ]
Correct Answer: Option B

A delete count of 0 removes nothing (so splice returns an empty array) and inserts the new items at index 1.

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