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
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read