JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – Did really string substring method can do this

JavaScript Quiz – Did really string substring method can do this

String Substring Method –

JavaScript string substring method can able extract the required characters from the string which is said to be a substring as it’s a part of the main string.

You need to provide starting and ending indices to the substring method to take out particular substring from the main string.

Point to be noted that it does not change the original string from which substring is taken. It’s syntax is look like.

string.substring(start, end)

To know more about JavaScript String Substring Method Click Here

JavaScript Question

Now, in this javascript quiz code you need to find out the output of substring getting from main string after passing given indices to it.

First, we declare a variable name using let keyword and assign a string to it which is “jsstartup.com“.

let name = "jsstartup.com";

In the next step, we create another variable getName using let and assign a value return from the substring method and we are passing start indices 0 and end indices 9.

let getName = name.substring(0 ,9);

Finally, we are doing console log of getName variable. Did you guess what is the output it print.

Answer – “jsstartup

Click To Run Code

Explanation

Now, you know the answer is “jsstartup” but why. So let me explain.

As we are using a substring method with name variable which contain a “jsstartup.com” as string. As, when we pass a start value 0 and end value 9 to substring method.

Then at 0 indices, j character in name string and it goes up to 9th indices on name variable which is p.

So, the final value which you get in getName is look like.

jsstartup

And it’s doesn’t include .com as they are after 9th indices.

That’s why we get an answer “jsstartup“.

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 – What parseInt function return if pass string
  2. JS Quiz – What happens when add or sub null value
  3. JavaScript Quiz – what really spread syntax with array pushing
  4. JavaScript Quiz – what is really object entries method return

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
4 Shares
Share via
Copy link