How to use Array.prototype.map() In Javascript
Using Array.prototype.map() with the string, each character is mapped to itself, effectively creating an array of characters. This concise method simplifies the conversion of a string into an array while retaining each character as an element in the array.
Example:
let str = 'Hello, World!';
let arr = Array.prototype.map.call(str, char => char);
console.log(arr); // ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
Output
[ 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!' ]
String to Array in JavaScript
In this article, you will learn how to convert a string to an array data type in JavaScript.
There are various methods to convert a String to an Array that is described below:
Table of Content
- Spread Operator
- Naive Method
- String Split() Method
- Array from() method
- Using slice() Method
- Using Array.prototype.map()