JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – string split method really a case sensitive

JavaScript Quiz – string split method really a case sensitive

Javascript string split method –

In javascript, string split method is used to break the string into multiple pieces based on the sub-string passed as parameter and return an array.

An array that returns from the javascript string split method contains substring of the main string.

It’s syntax look like –

string.split([separator[, limit]])

For more detail about javascript string split method click here.

JavaScript Question

In this javascript quiz code, let’s find out does string split method really a case sensitive which mean small and capital letter is not the same.

First, we declare a variable string using the let keyword and assign a javascript string value to it which is “Javascript (JS) Startup

let string = “Javascript (JS) Startup”;

In the next step, we create an another variable array using let and assign an array return from string split method.

let array = string.split(“s”);

As you can see, we pass a substring “s” as a separator. Now finally, we are checking the length of the array. Any guess, what should be the output.

console.log(array.length);

Answer – 2

Click To Run Code

Explanation

Now, you know the answer is 2 but why as we can see there are three “s” in the sting. Then the answer should be 3 or either 4 but not 2.

Let me explain –

let string = “Javascript (JS) Startup”;

Yes, it’s correct there are three “s” character in string variable but if you don’t know. Then i want to tell you that javascript is case sensitive.

That means, in javascript capital “S” and small “s” is never equal. So, when we pass “s” in separator. We get only two element in array.

[“Java”, “cript (JS) Startup”]

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

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 – can break statement work on array forEach method
  2. JS Quiz – variable can be key and value in object
  3. JavaScript Quiz – array filter method can filter odd value
  4. JS Quiz – var in a block scope work

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