JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – can string split method will be limited

JavaScript Quiz – can string split method will be limited

String Split Method –

The javascript string split method is used to split the javascript string into multiple pieces. Split() returns an array that contains substring of the main string as an element in the array.

Based on the separator, passed to split(separator) will be breaking the string into substring where it matches.

It’s syntax look like –

string.split(separator, limit)

Note: split method not change the original string in this process.

There is another parameter limit which is optional and it decides the number of splits in the string and returns a new array.

For more information about split string method click here.

JavaScript Question

In this javascript quiz, can string method will be limited to the number of splits as the split method has two arguments separator and limit.

First, declare a variable name and assign javascript string to it.

let name = “Hi i am jsstartup”;

In the second step, create another variable array and assign a result return from the split method used on a name with blank space separator and limit two.

let array = name.split(” “, 2);

Finally, checking the value of array index 3 as we know array variable contain a javascript array.

console.log(array[3]);

Any guess, what should be the output of above statement ?

Answer – undefined

Click To Run Code

Explanation

Now, you know the answer is undefined but why it should be “jsstartup”. Let me explain –

So, we use a split method and passes two parameter, as separator which is ” “ blank space and limit which is 2.

After split, array variable holding only 3 element.

let array = [“Hi”, “i”, “am”];

Split Limit Parameter-

But where is “jsstartup” also? As we put limit 2 on the split method so it will break the string up to 2 matching substring. It’s like –

“Hi(1)i(2)am(3)jsstartup”

And due to limit, it only include “Hi”, “i” & “am”.

At last, That’s why we get an answer “undefined“.

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. JS Quiz – new replaceAll method really replace all matching string
  2. JS Quiz – can it uppercase the string first letter
  3. JavaScript Quiz – does arguments is a default function parameter
  4. JavaScript Quiz – object seal method can restrict value change

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