LearnFrenzy
Sign in Take a free test

String Methods · Predict the Output · Question 6

split with a limit and a missing separator Answer, explanation & discussion

  • Output
  • Medium
  • 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
console.log("a-b-c".split("-", 2), "abc".split("-"), "a-b-c".split("-", 0));
  • A [ 'a', 'b-c' ] [] []
  • B [ 'a', 'b' ] [ 'a', 'b', 'c' ] [ 'a-b-c' ]
  • C [ 'a', 'b' ] [ 'abc' ] [ 'a', 'b', 'c' ]
  • D [ 'a', 'b' ] [ 'abc' ] [] Correct
Correct Answer: Option D

The limit caps the result; when the separator is not found the whole string is the only piece; a limit of 0 returns an empty array.

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