Sling Academy
Home/JavaScript/How to Convert a String into an Array in JavaScript

How to Convert a String into an Array in JavaScript

Last updated: February 28, 2023

In JavaScript, you can use the built-in split() method to split a string into an array of substrings based on a specified delimiter:

string.split(separator, limit)

Parameters

ParameterDescription
separatorOptional. Specifies the character(s) or regular expression used to split the string. The default value is an empty string (“”).
limitOptional. Specifies the maximum number of splits to be made.

Examples

Split a string with commas and spaces

The code:

const colors = "red, green, blue, yellow, orange, purple, pink"

const colorArray = colors.split(", ")
console.log(colorArray)

Output:

[
  'red',    'green',
  'blue',   'yellow',
  'orange', 'purple',
  'pink'
]

Using split() with the limit parameter

The code:

let str = "apple,banana,slingacademy.com";
let arr = str.split(",", 2);
console.log(arr);

Output:

["apple", "banana"]

In the example above, the split() method is used with a limit parameter of 2, which splits the string into an array of 2 substrings.

Using the split() method with regular expressions

You can use regular expressions to create complex and complicated splitting patterns, such as splitting a string based on any whitespace character (spaces, tabs, line breaks):

let str = "Welcome to slingacademy.com. This website is dedicated to learning JavaScript. We have a lot of JavaScript tutorials for beginners and advanced learners. We also have a lot of JavaScript projects for you to practice your JavaScript skills.";

let arr = str.split(/\s+/);
console.log(arr);

Output:

[
  'Welcome',     'to',         'slingacademy.com.',
  'This',        'website',    'is',
  'dedicated',   'to',         'learning',
  'JavaScript.', 'We',         'have',
  'a',           'lot',        'of',
  'JavaScript',  'tutorials',  'for',
  'beginners',   'and',        'advanced',
  'learners.',   'We',         'also',
  'have',        'a',          'lot',
  'of',          'JavaScript', 'projects',
  'for',         'you',        'to',
  'practice',    'your',       'JavaScript',
  'skills.'
]

Alternatives

Besides the split() method, there are some other solutions for splitting a string into an array. However, they all have limitations and only fit some special use cases.

Using the spread operator

Example:

let str = "hello";
let arr = [...str];
console.log(arr);

Output:

["h", "e", "l", "l", "o"]

Using Array.from()

Example:

let str = "hello";
let arr = Array.from(str);
console.log(arr);

Output:

["h", "e", "l", "l", "o"]

I hope this helps! Leave a comment if you have any more questions.

Next Article: 3 Ways to Validate an Email Address in JavaScript

Previous Article: 4 Ways to Extract a Substring from a String in JavaScript

Series: JavaScript Strings

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration