What is "1,000,000".replace(",", "")?
A string pattern replaces only the first occurrence. Use replaceAll(",", "") or replace(/,/g, "") to remove every comma.
String Methods · Exercise 1 of 4
Fifteen multiple-choice questions on JavaScript string methods: replace and replaceAll, padStart, trim, split with limits, match and matchAll, search, includes positions, localeCompare, normalize, regex flags and lastIndex. Pick an option to check your answer instantly.
What is "1,000,000".replace(",", "")?
A string pattern replaces only the first occurrence. Use replaceAll(",", "") or replace(/,/g, "") to remove every comma.
What happens with "a.b.c".replaceAll(/\./, "-")?
Calling replaceAll with a non-global regex would be contradictory, so it throws. Write /\./g – or pass the plain string ".".
Which expression turns the day number 5 into "05"?
padStart is a string method, so convert the number first. padEnd would give "50"; numbers have no padStart method.
What is "\t Asha Rao \n".trim()?
trim removes all whitespace – spaces, tabs, line breaks – from both ends, but never inside the string.
What does "a:b:c:d".split(":", 2) return?
The limit caps the number of pieces returned; the rest of the string is discarded (unlike some other languages, where the last piece keeps the remainder).
What does "x1y22".match(/(\d)+/g) return?
With the g flag match returns every full match as a string. Without g it returns the first match plus its capture groups and index.
Which statement about str.matchAll(regex) is correct?
matchAll throws a TypeError for a non-global regex. When nothing matches, the iterator is simply empty – spread it ([...str.matchAll(re)]) to get an array.
What is the main difference between search and indexOf?
"Room 101".search(/\d/) is 5. indexOf("1", 6) can start later in the string, which search cannot.
Which comparator sorts ["img12", "img2", "img1"] as img1, img2, img12?
The numeric option compares digit sequences as numbers. Default sort compares characters, so "img12" comes before "img2"; subtracting strings gives NaN.
Two strings both display as "José" but a === b is false. What is the likely cause and fix?
Unicode allows composed and decomposed forms. normalize("NFC") (the default) converts both to the composed form, so equal text compares equal.
Timed online tests with instant scores and explanations.