JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – does replace really work on replaced string

JavaScript Quiz – does replace really work on replaced string

JavaScript Replace String

Javascript replace string is a string method for replacing a matching string and return a new string. But if matching string not matched then return a original string.

Basic Syntax of Replace Method

str.replace(regexp|substr, newSubstr|function)

For more detail about the string replace method click here.

JavaScript Question

In this javascript quiz, find out does replace really work on replaced string because replace change the match string if substring is matched

First, we declare a variable name using let keyword and assign an javascript string to it.

let name = "JS Startup";

Now, we use a replace method and replace a “JS” with “Node” and print it’s value using console log.

console.log(name.replace("JS", "Node")); 

Now, we again use a replace method and replace a “Node” with “React” and same print the value with console log.

console.log(name.replace("Node", "React")); 

Any idea, what should be the output of above statement.

JavaScript Quiz Test

Here is the full javascript code snippet for you to solve.

let name = "JS Startup";

console.log(name.replace("JS", "Node")); 
// output => "Node Startup"

console.log(name.replace("Node", "React")); 
// output => 🧐 ?

// Made with ❤️, by @jsstartup

Answer – “JS Startup”

Click To Run Code

Explanation

Finally, you know the answer is “JS Startup“. But why, doesn’t it should replace with “React“. Let me explain.

InShort, the reason is that replace does not modify the origin string.

Due to which, when we replace a “JS” with “Node”. Then, str value is still “JS Startup”.

So, when we apply the replace method again to replace “Node” with “React” but “Node” not exist in str value. That’s why it return the original string.

At last, That’s why we get an answer “JS Startup”

Let me know in the comment section, what you think about 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. JavaScript Quiz – do act then while checking falsy
  2. JS Quiz – for loop iterate array in two ways
  3. JS Quiz – array element duplicate count with for loop
  4. JavaScript Quiz – isArray method and typeof intend is different

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