Split String into an Array using JavaScript

   JavaScript

Using the .split() method, we can split string into an array. The key point here is the “separator”, you need to choose the unique separator identity as per given string.

Solution

1st Example

var str = "I Love JavaScript";
var arr = str.split(" "); // The unique separator is an empty space
console.log(arr);

//Output
["I", "Love", "JavaScript"]

2nd Example

var month = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec";
var monthArr = month.split(","); // The unique separator is ","
console.log(monthArr);

//Output
["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
Tags: ArrayJavaScriptSplitSplit into an array

Leave a Comment