JavaScript split string into array

There are multiple ways to split string into array using JavaScript. We will cover a few ways to split it along with some examples.

Split string using loop

We need to have a For Loop running on our String length. We will put a condition to check for the delimiter character. Delimiter character could by anything eg. a condition like split by capital case, a dot (.), a comma (,) etc. Let’s understand it with some examples.

I have incorporated below the splitting behavior of string using For Loop by various conditions.

Conditions below incorporated are:

  • Split string into array by Capital case using For Loop
  • Split string into array by Comma using For Loop

Split string using in-built method

There is a split method in JavaScript to work with string. We will use this method to split our string into Array based on a delimiter condition. I have incorporated split method in below example code.

Below code consists:

  • Split string by Capital case into Array using split method. I have used a small Regex /(?=[A-Z])/ to match for Capital Case.
  • Split string by Comma into Array using split method.

That is all for this blog.

Write me up for any queries you have for me.

Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *