How to Convert a String to an Array with JavaScript

In this tutorial, we will learn how to use four different JavaScript methods to convert a String to an Array. The split() method allows us to split a string into an array of substrings based on a specified delimiter. The Object.assign() method allows us to copy the values of all enumerable own properties from one or more source objects to a target object. The Spread operator allows us to expand elements of an array or object in places where multiple elements or properties are expected. The Array.from() method allows us to create a new array instance from an array-like or iterable object.

Convert a String to an Array with JavaScript

In this tutorial, we will look at four different ways to convert strings into arrays using JavaScript. Those ways include split(), Object.assign, Spread, and Array.from methods.


split()

To convert a string to an array with JavaScript, you can use the split() method. This method takes a delimiter string as an argument and returns an array of substrings.

split() is a JavaScript method that is used to split a string into an array of substrings. It takes a string and a delimiter as arguments, and returns an array of substrings that were created by splitting the original string using the delimiter as a separator.

Here is an example of how you might use the split() method to convert a string to an array:

// define a string with a space delimiter
let string = "hello world";

// convert the string to an array of substrings
let array = string.split(" ");

// log the array to the console
console.log(array); // ["hello", "world"]

In this example, we define a string called string with the value "hello world". We then use split() to convert the string to an array of substrings, using a space character (" ") as the delimiter. This returns an array with two elements, "hello" and "world", which are the substrings in the original string.

You can also use regular expressions as the delimiter for split(). Regular expressions provide a powerful and flexible way to specify the pattern that the delimiter should match in the original string.

Here is an example of how you might use a regular expression as the delimiter for split():

// define a string with a space or comma delimiter
let string = "hello,world";

// convert the string to an array of substrings using a regular expression
let array = string.split(/[ ,]+/);

// log the array of substrings to the console
console.log(array); // ["hello", "world"]

In this example, we define a string called string with the value "hello,world". We then use split() to convert the string to an array of substrings, using a regular expression as the delimiter. The regular expression /[ ,]+/ specifies that the delimiter should match one or more space or comma characters. This returns an array with two elements, "hello" and "world", which are the substrings in the original string.

Object.assign

To convert a string to an array with JavaScript using the Object.assign() method, you can use split() in combination with Object.assign(). The split() takes a delimiter string as an argument and returns an array of substrings. The Object.assign() copies the values of all enumerable own properties from one or more source objects to a target object.

Object.assign() is a JavaScript method that is used to copy the values of all enumerable own properties from one or more source objects to a target object. It takes a target object and one or more source objects as arguments, and returns the target object after copying the properties from the source objects.

Here is an example of how you might use split() and Object.assign() to convert a string to an array:

// define a string with a space delimiter
let string = "hello world";

// split the string into an array of substrings
let array = string.split(" ");

// create an empty object
let object = {};

// convert the array to an object using Object.assign()
Object.assign(object, array);

// log the object to the console
console.log(object); // { "0": "hello", "1": "world" }

In this example, we define a string called string with the value "hello world". We then use the split() method to split the string into an array of substrings, using a space character (" ") as the delimiter. This returns an array with two elements, "hello" and "world", which are the substrings in the original string.

Next, we create an empty object called object. We then use Object.assign() to copy the values of the elements in the array to the object. This converts the array to an object, where the keys are the indices of the elements in the array and the values are the elements themselves. The resulting object is { "0": "hello", "1": "world" }, which is the converted string as an object.

You can also use Object.assign() to convert an array of objects to an object, where the keys are the property values of the objects in the array and the values are the objects themselves.

Spread

To convert a string to an array with JavaScript using the spread operator (...), you can use the split() method in combination with the spread operator. The split() method takes a delimiter string as an argument and returns an array of substrings. The spread operator allows you to expand an iterable object (such as an array) into its individual elements.

The spread operator (...) is a JavaScript syntax that is used to expand or spread an iterable object (such as an array or a string) into individual elements or characters. It is often used to combine or merge multiple arrays or to convert a string into an array of characters.

Here is an example of how you might use split() and the spread operator to convert a string to an array:

// define a string with a space delimiter
let string = "hello world";

// split the string into an array of substrings
let array = string.split(" ");

// convert the array to an array using the spread operator
let spread = [...array];

// log the array to the console
console.log(spread); // ["hello", "world"]

In this example, we define a string called string with the value "hello world". We then use split() to split the string into an array of substrings, using a space character (" ") as the delimiter. This returns an array with two elements, "hello" and "world", which are the substrings in the original string.

Next, we use the spread operator (...) to expand the elements of the array into a new array called spread. This creates a new array with the same elements as the original array, which is the converted string as an array.

The spread operator allows you to expand the elements of an array into a new array and provides a simple and concise way to create arrays from strings.

Array.from()

To convert a string to an array with JavaScript using the Array.from() method, you can pass the string as an argument to Array.from(). This creates a new, shallow-copied Array instance from an array-like or iterable object.

Array.from() is used to create an array from an iterable object (such as an array, a string, or an array-like object). It takes an iterable object and optional map and filter arguments, and returns a new array with the elements from the iterable object.

Here is an example of how you might use the Array.from() method to convert a string to an array:

// define a string
let string = "hello world";

// convert the string to an array using Array.from()
let array = Array.from(string);

// log the array to the console
console.log(array); // ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]

In this example, we define a string called string with the value "hello world". We then use Array.from() to convert the string to an array. This creates a new array with the individual characters of the string as elements, resulting in the array ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"].

You can also use Array.from() to convert a string to an array of substrings, by passing a delimiter string as the second argument to Array.from(). The delimiter string specifies the original string's characters that separate the substrings.

Here is an example of how you might use Array.from() to convert a string to an array of substrings:

// define a string with a space delimiter
let string = "hello world";

// convert the string to an array of substrings using Array.from()
let array = Array.from(string, x => x.split(" "));

// log the array of substrings to the console
console.log(array); // ["hello", "world"]

In this example, we define a string called string with the value "hello world". We then use Array.from() to convert the string to an array of substrings, using a space character (" ") as the delimiter. This creates a new array with two elements, "hello" and "world", which are the substrings in the original string.