JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – new replaceAll method really replace all matching string

JavaScript Quiz – new replaceAll method really replace all matching string

String replaceAll Method –

In javaScript, the replaceAll string method is introduced which can change all matching string (substring) with the given string.

Previously, replace method is used but it only replace first matching string but not other. For replace all matching string you have to pass regex.

But that not the case with the replaceAll() string method but it still supports regex also.

It’s syntax looks like –

const newStr = str.replaceAll(regexp|substr, newSubstr|function)

For more information about Javascript replaceAll Method click here.

JavaScript Question

In this javascript quiz, find out does new replaceAll method really replace all matching substring as previously regex is needed.

First, we declare a variable name with let keyword and assign a javascript string to it which is “JS Startup“.

let name = “JS Startup”;

In the second step, we create an another variable string using let and assign the string return from string replaceAll method.

let string = replaceAll(“t”,”i”);

Above, we are going to replace substring “t” with “i“.

Finally, we are checking the output of string using console log.

console.log(string);

Any idea what should the output of it.

Answer – “JS Siariup”

Click To Run Code

Explanation

So, Finally you know the answer is “JS Siariup“. But why, didn’t previously it replace the first occurrence only. Let me explain.

First, it a string replaceAll method not a replace method. In the replace method, we need to pass regex if you want to replace all matching substring.

But replaceAll method, replace all the matching string without any regex. It’s a new method which is introduced.

Note: JavaScript is a case sensitive the means in above snippet it only replace small letter of “t” not the capital letter “T”.

At last, That’s why we get an answer “JS Siariup“.

Let me know in the comment section, any alternative of it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – can reduceRight method really sum an array elements
  2. JS Quiz – includes method can really remove duplicates with forEach
  3. JavaScript Quiz – can delete operator delete a global variable
  4. JavaScript Quiz – add new key in object after freeze is possible

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link