String Split() Method

The String Split() Method takes the pattern as an argument and returns the target string as a list or array. The separator represents the function where to split and the limit represents the number of substrings or lengths up to which the string will be processed.

Syntax:

split(separator); // or
split(separator, limit);

Example:

Javascript
// Declare a String
let str= "w3wiki";

// Use String split() method to 
// Convert String to an Array
let arr = str.split('');

console.log(arr);

Output
[
  'G', 'e', 'e', 'k',
  's', 'f', 'o', 'r',
  'G', 'e', 'e', 'k',
  's'
]




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()

Similar Reads

Spread Operator

The Spread Operator represented by (…) allows any iterable to expand like an array into individual elements i.e. string to characters....

Naive Method

This approach includes traversing every character of the string and pushing that to the array....

String Split() Method

The String Split() Method takes the pattern as an argument and returns the target string as a list or array. The separator represents the function where to split and the limit represents the number of substrings or lengths up to which the string will be processed....

Array from() method

The Array from() method returns a new instance of an array from the object provided in the arguments....

Using slice() Method

The call() method is used to invoke the slice() method, treating the string as this value. This creates a new array where each character in the string is an element....

Using Array.prototype.map()

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....