Converting String to Array in JavaScript

To convert a string into an array in JavaScript, you can use the string.split(separator, limit), array.from(string) methods and the spread ("...") operator. The most commonly used method is the string.split(). It converts a string into an array of strings by splitting it with a "separator". The "separator" can be a character, a string, or a regular expression. The optional "limit" parameter specifies how many times to split the string. The array.from() method and the spread ("...") operator convert a string into an array of characters. In this JavaScript String to Array conversion example, we use the string.split() method to convert a string into an array of strings. Below you can see more examples of converting strings to arrays in JavaScript, including Regular Expressions. Click Execute to run the JavaScript String to Array Example online and see the result.
Converting String to Array in JavaScript Execute
const str = 'JavaScript String to Array Example';

const result = str.split(' ', 4);

console.log(result);
Updated: Viewed: 5935 times

What is JavaScript?

JavaScript is a scripting language that works both on the client side (in browsers) and on the server side (Node.js). JavaScript is the main programming language for the web. Using JavaScript, you can change HTML code and CSS styles, process data, play videos, submit requests, validate forms, and more.

What is a string in JavaScript?

A JavaScript string is a sequence of characters, including letters, numbers, and symbols. Unicode characters can be encoded in JavaScript strings using the "\uXXXX" syntax. In JavaScript, strings are primitive, immutable data types, which means they cannot be changed once they are created. JavaScript provides built-in functions for splitting, concatenating, containing, trimming, and reversing strings. You can also convert an array to a string, check if it starts or ends with another string, create a multiline string, and get the length of a string.

What is an array in JavaScript?

In JavaScript, an array is a special built-in object that allows multiple elements of different types to be stored in a single variable. JavaScript arrays can be created with square brackets "[...]" or using the "new Array()" constructor. Each array element has an index, allowing you to access them directly by its index or loop through the array with a "for loop". JavaScript provides many built-in functions for reducing, reversing, merging, joining, slicing, containing, cloning, inserting, clearing, and copying arrays. You can also get a sum of array elements and convert the array to JSON.

How to convert string to array in JavaScript?

In JavaScript, you can use several ways to convert a string to an array. The most popular method is string.split(separator, limit). The string.split() method is used to split a string into an array of substrings based on a specified delimiter, where the delimiter can be a character, a string, or a Regular Expression. You can also use the array.from() method and the spread ("...") operator to convert a string to an array of characters. The difference between the split() and from() methods and the spread operator is that the string.split() is used to split a string into an array of substrings, and the array.from() is used to create a new array from an array-like or iterable object.

JavaScript String to Array Example
const str = 'JavaScript Array';

const result = str.split(' ');

console.log(result);

// output: ['JavaScript', 'Array']

JavaScript String to Array Examples

The following are examples of converting a string to an array in JavaScript:

Converting string to an array using the string.split() method

The string.split(separator, limit) method is used in JavaScript to convert a given string into an array of strings using a separator. The second parameter of the split() method specifies how many times to split the matched value; if no limit is specified, the string will be split into all matching values. The string.split() method returns a new array without changing the original string.

JavaScript Convert String to Array with split()
const str = 'JavaScript String to Array Example';

console.log(str.split(' ', 1));

// output: ['JavaScript']

Converting multiline string to an array

To convert a multiline string to a JavaScript array, you can use the split() method and specify newlines "\n" as the separator:

JavaScript Convert Multiline String to Array
const str = `JavaScript
String to Array
Example`;

console.log(str.split('\n'));

// output: ['JavaScript', 'String to Array', 'Example']

Converting string with special characters to an array

The following is an example of converting a string with special characters to an array using the split() method and the "," separator:

JavaScript Convert String with Special Characters to Array
const str = "JavaScript,String,to,Array";

console.log(str.split(",")); 

// output: ['JavaScript', 'String', 'to', 'Array']

JavaScript String to Array with Regular Expressions

The following are examples of converting a string to an array with Regular Expressions in JavaScript:

Converting string to an array using the Regular Expressions

To convert a string to an array using Regular Expressions, you can use the split() method and specify a Regular Expression as the delimiter:

JavaScript Convert String to Array with Regex
const str = 'JavaScript String to Array Example';

const result = str.split(/,\s*/);

console.log(result);

// output: ['JavaScript String to Array Example']

Converting multiline string to an array using the Regular Expressions

To convert a multiline string to an array using Regular Expressions, you can use the match() method. The match() method accepts a Regular Expression as an argument:

JavaScript Convert Multiline String to Array with Regex
const str = `JavaScript
String to Array
Example`;

console.log(str.match(/[^\r\n]+/g));

// output: ['JavaScript', 'String to Array', 'Example']

Converting string with special characters to an array using Regular Expressions

To convert a string with special characters to an array using Regular Expressions, you can use the match() method, which accepts a Regular Expression as an argument:

JavaScript Convert String with Special Characters to Array using Regex
const str ='example1,example2,"example,3",example4';

console.log(str.match(/(".*?"|[^",]+)(?=\s*,|\s*$)/g));

// output: ['example1', 'example2', '"example,3"', 'example4']

Converting string to an array using the from() method

The Array.from() method in JavaScript is used to create an array from any iterable object. The from() method returns a new array instance whose elements correspond to each element in the iterable. In the case of a string, each character of the string is converted to an array element.

JavaScript Convert String to Array with from()
const str = 'JavaScript';

console.log(Array.from(str));

// output: ['J', 'a', 'v', 'a', 'S', 'c', 'r', 'i', 'p', 't']

Converting string to an array using the spread operator

In JavaScript ES6 and above, you can use the spread ("...") operator to convert a string to an array.

JavaScript Convert String to Array with Spread Operator
const str = 'JavaScript';

console.log([...str]);

// output: ['J', 'a', 'v', 'a', 'S', 'c', 'r', 'i', 'p', 't']

How to convert array to string in JavaScript?

To convert an array to a JavaScript string, you can use the array.toString() method:

JavaScript Convert Array to String Example
let arr = ['JavaScript', 'String'];

console.log(arr.toString());

// output: JavaScript,String

See also