JavaScript – Convert String to Array
Last Updated :
28 Apr, 2025
Strings in JavaScript are immutable (cannot be changed directly). However, arrays are mutable, allowing you to perform operations such as adding, removing, or modifying elements. Converting a string to an array makes it easier to:
- Access individual characters or substrings.
- Perform array operations such as
map
, filter
, or reduce
. - Modify parts of the string (like replacing characters or extracting substrings).
- Split a string into smaller components for easier processing.
Methods to Convert String to Array
1. Using the String.split() Method
The most common method for converting a string to an array is using the split()
method. It allows you to specify a delimiter (separator) that divides the string into an array of substrings.
Syntax
string.split(separator, limit);
In the above syntax
- separator: Defines how to split the string. It can be a character, a string, or even a regular expression.
- limit: Optional. Limits the number of splits in the array.
JavaScript
const str = "Hello";
const arr = str.split('');
console.log(arr);
Output[ 'H', 'e', 'l', 'l', 'o' ]
2. Using Spread Operator(…)
The spread operator (...
) can be used to convert a string into an array by spreading the string into individual characters. This method is concise and expressive, and is often preferred for simple tasks.
Syntax
const arr = [...string];
In the above syntax
- […string]: It spreads each character of the string into individual elements in an array.
JavaScript
const str = "world";
const array = [...str];
console.log(array);
Output[ 'w', 'o', 'r', 'l', 'd' ]
This method also handles Unicode characters (like emojis) properly.
JavaScript
const fruit = '🍎🍎';
const fruitArr = [...fruit];
console.log(fruitArr);
3. Using Array.from() Method
The Array.from()
method can convert a string into an array of characters. This method is especially useful if you want to create an array from an iterable object, like a string, without needing to specify a separator.
Syntax
Array.from(string);
In the above syntax
- Array.from(string): Converts an iterable object (such as a string) into an array.
JavaScript
const str = "JavaScript";
const array = Array.from(str);
console.log(array);
Output[
'J', 'a', 'v', 'a',
'S', 'c', 'r', 'i',
'p', 't'
]
we can also use a mapping function directly with Array.from() The mapping function takes three arguments:
- The current element.
- The index of the element.
- The array being created.
JavaScript
const s = "Hello";
const a = Array.from(s, char => char.toUpperCase());
console.log(a);
Output[ 'H', 'E', 'L', 'L', 'O' ]
4. Using slice() with call()
The slice() method is typically used for arrays, but you can apply it to a string by borrowing the method using call().
Syntax
Array.prototype.slice.call(string);
In the above syntax
- The slice() method is originally an array method.
- When combined with call(), it can be used on array-like objects, such as strings, to create a new array.
- It treats the string as if it were an array and returns a shallow copy as an array.
JavaScript
const s = "Hello";
const a = Array.prototype.slice.call(s);
console.log(a);
Output[ 'H', 'e', 'l', 'l', 'o' ]
5. Using Object.assign()
Object.assign() copies the properties from one or more source objects into a target object. When used with an empty array, it can convert a string into an array of characters.
Syntax
Object.assign([], string);
In the above syntax
- Object.assign(): A method used to copy enumerable properties from source objects to a target object.
- []: An empty array, serving as the target object.
- string: The source string whose individual characters are treated as properties and copied into the target array.
JavaScript
const myFavBook = 'GeeksforGeeks';
const myFavBookArray = Object.assign([], myFavBook);
console.log(myFavBookArray);
Output[
'G', 'e', 'e', 'k',
's', 'f', 'o', 'r',
'G', 'e', 'e', 'k',
's'
]
6. Using Array.prototype.map() Method
The Array.prototype.map() method is a powerful tool in JavaScript for transforming or modifying the elements of an array. While map() works directly on arrays, we can also use it to map over a string by first converting the string into an array. Once converted, we can apply the map() method to manipulate or transform each character in the string.
Syntax
Array.prototype.map.call(string, char => char);
In the above Syntax
- Array.prototype.map: The array method used to create a new array by applying a function to each element.
- call: Allows the use of array methods on array-like objects (such as strings).
- string: The array-like object (the string) to be iterated over.
- char => char: The callback function applied to each character, returning each character directly into the new array.
JavaScript
const s = "Hello";
const a = [...s].map(char => char.toUpperCase());
console.log(a);
Output[ 'H', 'E', 'L', 'L', 'O' ]
7. Using a for Loop
If we need more control over the process or need to apply specific transformations while converting a string to an array, we can use a for
loop. This method allows us to manually push each character or part of the string into an array.
JavaScript
const str = "Hello";
const arr = [];
for (let i = 0; i < str.length; i++) {
arr.push(str[i]);
}
console.log(arr);
Output[ 'H', 'e', 'l', 'l', 'o' ]
When to Use Which Approach?
Here’s a quick comparison of different methods for breaking or transforming strings in JavaScript and when you should choose each approach:
Approach | When to Use |
---|
String.split() | Best for splitting strings based on a specific delimiter (e.g., spaces, commas). |
Spread Operator | Quick and simple for breaking a string into characters. |
Array.from() | Flexible for converting strings and applying transformations. |
slice() | Works effectively but is less commonly used for this purpose. |
Array.prototype.map() | Ideal when mapping or transforming the characters is required. |
for Loop
| Common and flexible approach, providing full control over the iteration process.
|
Conclusion
Each of the methods above offers a simple way to convert a string to an array in JavaScript. The choice of method depends on the specific task at hand, such as whether you need to split based on a delimiter, transform the string, or handle Unicode characters like emojis.
Similar Reads
How to Convert String of Objects to Array in JavaScript ?
This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i
4 min read
Convert comma separated string to array using JavaScript
Here are the various methods to convert comma-separated string to array using JavaScript. 1. Using the split() Method (Most Common)The split() method is the simplest and most commonly used way to convert a comma-separated string into an array. It splits a string into an array based on a specified ch
3 min read
JavaScript - How to Get Character Array from String?
Here are the various methods to get character array from a string in JavaScript. 1. Using String split() MethodThe split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. [GFGTABS] JavaScript let
2 min read
Set to Array in JS or JavaScript
This article will show you how to convert a Set to an Array in JavaScript. A set can be converted to an array in JavaScript in the following ways: Using Spread OperatorThe JavaScript Spread Operator can be used to destructure the elements of the array and then assign them to the new variable in the
2 min read
How to convert Integer array to String array using JavaScript ?
The task is to convert an integer array to a string array in JavaScript. Here are a few of the most used techniques discussed with the help of JavaScript. Approaches to convert Integer array to String array:Table of Content Approach 1: using JavaScript array.map() and toString() methodsApproach 2: U
2 min read
JavaScript - Convert a Number into JS Array
You have a number, like 12345, and you need to convert it into an array where each element represents a digit of the number. For example, 12345 should become [1, 2, 3, 4, 5]. How can you achieve this in JavaScript? In JavaScript, there are various ways to transform a number into an array of its digi
3 min read
How to Convert a Map to JSON String in JavaScript ?
A Map is a collection of key-value pairs, where each key is unique. In this article, we will see how to convert a Map to a JSON (JavaScript Object Notation) string in JavaScript. However, JSON.stringify() does not directly support Map objects. Table of Content Using Object.fromEntries() MethodUsing
2 min read
JavaScript Array toString() Method
The toString() method returns a string with array values separated by commas. The toString() method does not change the original array. This method converts an array into a readable string format, often for display or logging purposes. Syntax: arr.toString()Parameters: This method does not accept an
3 min read
JavaScript Array from() Method
The Array.from() method is used to create a new array from any iterables like array, objects, and strings. [GFGTABS] JavaScript const a = Array.from("GeeksforGeeks"); console.log(a); [/GFGTABS]Output[ 'G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's' ] Syntax Array.from(objec
2 min read
JavaScript Array from() Method
The JavaScript Array from() method returns an Array object from any object with a length property or an iterable object. Syntax : Array.from(object, mapFunction, thisValue)Parameters:object: This Parameter is required to specify the object to convert to an array.mapFunction: This Parameter specifies
3 min read