Converting string to array in JavaScript?

Using split()

var str="google" var array = str.split(''); //[ 'g', 'o', 'o', 'g', 'l', 'e' ]

Convert string with commas to array (Specific Separators)

var str="g,o,o,g,l,e" var array = str.split(","); //[ 'g', 'o', 'o', 'g', 'l', 'e' ]
var str="google microsoft facebook" var array = str.split(" "); //[ 'google', 'microsoft', 'facebook' ]
There are other methods to convert string to array. You can convert string to array javascript without using split() method.

Using Array.from()

var str="google" var array = Array.from(str); //[ 'g', 'o', 'o', 'g', 'l', 'e' ]

Using spread operator(…)

var str="google" var array = [...str]; //[ 'g', 'o', 'o', 'g', 'l', 'e' ]

Using Object.assign()

var str="google" var array = Object.assign([], str); //[ 'g', 'o', 'o', 'g', 'l', 'e' ]

Using JSON.parse()

var str = "0,1,2,3,4,5"; var array = JSON.parse("[" + str + "]"); //[ 0, 1, 2, 3, 4, 5 ]

Using RegEx

var str = "0,1,2,3,4,5"; var array = str.match(/\d+/g) //[ '0', '1', '2', '3', '4', '5' ]

JavaScript split()

JavaScript split() method splits a given string into an array of substrings. This method does not change the original string and returns the new array. If you specify a separator, the string is split between word(s). If specified separator is a non-empty string, the target string is split by all matches of the separator without including separator in the results. Syntax
string.split(separator, limit);

JavaScript Array.from()

Javascript String to array
JavaScript Array.from() method returns an array from any iterable object. It has an optional parameter mapFunction, which allows you to run a function on each element of the array being created, similar to map(). Syntax
Array.from(object, mapFunction, thisValue)

JavaScript spread operator(…)

The spread operator (...) allows you to quickly copy all or part of an existing array or object into another array or object. Syntax
var variablename1 = [...value];

JavaScript Object.assign()

JavaScript Object.assign() is used to copy the values and properties from one or more source objects to a target object. It returns the modified target object. Syntax
Object.assign(target, sources)


The JSON.parse() static method parses a JSON string, constructing the JavaScript value or object described by the string. Syntax