Convert a string to character array in JavaScript
This post will discuss how to convert a string to a character array in JavaScript.
There are several ways to convert a string to a character array in JavaScript, which is a common task when working with strings and arrays. Converting a string to a character array means creating an array that contains each character of the string as a separate element. For example, converting the string "Hello" to a character array would result in ["H", "e", "l", "l", "o"]. Here are some common functions:
1. Using String.split() function
A simple and widely supported way to convert a string to a character array is to use the split() function of the string object. This function divides a string into an array of substrings, using a specified separator or an empty string by default. If we use an empty string as the separator, we will get an array of single characters from the string. Here’s an example:
|
1 2 3 4 5 |
var str = "Hello"; var arr = str.split(""); console.log(arr); // ["H", "e", "l", "l", "o"] |
2. Using Array.from() function
Another way to convert a string to a character array is to use the Array.from() function. This function creates a new array from an iterable or array-like object. A string is an iterable object, so we can pass it directly to the Array.from() function and get an array of characters from it. The syntax is Array.from(object), where object is the name of the object to create the array from. Here’s an example:
|
1 2 3 4 5 |
var str = "Hello"; var arr = Array.from(str); console.log(arr); // ["H", "e", "l", "l", "o"] |
3. Using spread operator
A third way to convert a string to a character array is to use the spread operator (…). This operator allows us to expand an iterable object into an array of individual elements. If we use it on a string, it will create an array of characters. The syntax is […iterable], where iterable is the name of the object to spread. Here’s an example:
|
1 2 3 4 5 |
var str = "Hello"; var arr = [...str]; console.log(arr); // ["H", "e", "l", "l", "o"] |
This function is similar to the second one and does not modify the original string. However, this function requires ES6 support, which may not be available in older browsers.
4. Using Object.assign() function
The Object.assign() function copies the properties of one or more source objects to a target object, which can be an array. Its syntax is Object.assign(target, ...sources), where target is the name of the object to copy to and sources are the names of the objects to copy from. The function returns the modified target object. If we pass an empty array as the target object and a string as the source object, it will copy the characters of the string as the elements of the array. Here’s an example:
|
1 2 3 4 5 |
var str = "Hello"; var arr = Object.assign([], str); console.log(arr); // ["H", "e", "l", "l", "o"] |
That’s all about converting a string to a character array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)