LearnFrenzy
Sign in Take a free test

String Methods · Predict the Output · Question 1

replace vs replaceAll Answer, explanation & discussion

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

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

main.js
console.log("a-b-c".replace("-", "+"), "a-b-c".replaceAll("-", "+"), "a-b-c".replace(/-/g, ""));
  • A a+b+c a+b+c abc
  • B a+b-c a+b+c abc Correct
  • C a+b-c a+b-c a-b-c
  • D a+b-c a+b+c ab-c
Correct Answer: Option B

A string pattern in replace changes the first match; replaceAll and a /g regex change all of them.

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