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. 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: 1706 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, which turns static pages into interactive ones. Using JavaScript, you can change HTML code and CSS styles, process data, play videos, 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 array to 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.

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 string.split()
const str = 'JavaScript String to Array Example';

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

// output: ['JavaScript']

Converting a string with special characters to an array

In JavaScript, you can convert a string with special characters to an array using the split() method. The split() method splits a string into an array of substrings based on a specified separator:

JavaScript Convert String with Special Characters to Array
const str = 'JavaScript\nString\nto\nArray';

const result = str.split("\n");

console.log(result);

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

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']

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 Regular Expressions
const str = 'JavaScript String to Array Example';

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

console.log(result);

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

See also