String Split Method –
A separator is just a string that tells a split method to break the string. Wherever you found the separator string.
The limit is just an integer to limit the number of splits. For example, if you want to extract every piece of URL.
Then you use ‘/’ as a separator and you can put limit but mostly it not required.
In this quiz, you need to find out the length of split string.
First, we declare a variable name using let keyword and assign an ‘JS Startup’ string to it .
In the next step, we create another variable splitString using let and assign name.split().
Now, we have a name variable that contains a “JS Startup” and splitString variable which contains an array returned from the name.split() method.
Now, here you need to find out the length of string and splitString.
I know, it look confusing to you that why the answer is 10, 1
Let me explain you, first we are checking the length of name variable. Then it’s obviously 10. Because it contain a 9 alphabets and 1 blank space.
Now, comes the main part when we use a string split method and do nor pass any separator to it. Then it convert whole string as a separator.
And your splitString look like it after value return from name.split()
As you can see it’s an array with only one element in it. That’s why it’s length is 1.
But wait here i have something more for you.
- JS Quiz – What really the array values method return
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.