String Substring Method –
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.
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.
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.
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 –
- JS Quiz – What happens when add or sub null value
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.