JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScipt Quiz – What is the length of String Split Method

JavaScipt Quiz – What is the length of String Split Method

String Split Method –

string.split(separator, limit) is the syntax of the split string method. JavaScript string split is used for breaking the string into multiple pieces based on separator passed to it.

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.

Note: javascript string split method returns a javascript array.

For more info about Javascript String Split Method.

JavaScript Question

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.

Answer – 10, 1

Click To Run code

Explanation –

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()

[‘JS Startup’]

As you can see it’s an array with only one element in it. That’s why it’s length is 1.

I hope you understand the concept behind this javascript quiz.

But wait here i have something more for you.

Check out our other javascript quiz – 

  1. JavaScript Quiz – What actually negative infinity value returns
  2. JS Quiz – What really the array values method return
  3. JavaScript Quiz – Does nested array reference removed by spread syntax

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