JavaScript String to Array – How to convert string to array of characters

5 years ago Lalit Bhagtani 0

JavaScript String to Array

In this tutorial, we will learn about how to convert string to array of characters in JavaScript.

JavaScript String to Array

Array from Method :-

JavaScript Array objects contains a from method, which can be used to create array from any iterable object like string, array, map and set. If from method is invoked by passing string as an iterable argument, it returns an array containing string characters.

Syntax :-

Array.from( arrayLike [, mapFunction [, argument ] ] )

Let’s see the example :- 

<script>
var arr = Array.from('London');
console.log(arr);
</script>

Output :-

 ["l", "a", "l", "i", "t"]

String split Method :-

JavaScript String objects contains a split method, which can be used to split the given string to an array of substrings by using a separator string passed as an argument. If split method is invoked without passing any arguments, it split the string into an array of its characters.

Syntax :-

String.split([separator], [limit]);

Let’s see the example :- 

<script>
var string = "Hello World";
var array = string.split();
console.log(array);
</script>

Output :-

 ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]

References :- 

  1. Array From Docs
  2. String split Docs

If you liked it, please share your thoughts in comments section and share it with others too.