• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Convert String to Array in JavaScript

Convert String to Array in JavaScript

September 16, 2022 Leave a Comment

To convert a string to an array using JavaScript, the easiest way is with the JavaScript Array.from() method.

var someString = "string";

var stringToArray = Array.from(someString);

console.log(stringToArray);

#Output:
["s", "t", "r", "i", "n", "g"]

If we want to convert a string into an array of elements with a delimiter, we can use the split() method and pass what we want to split the string by as a parameter.

Here is our first example above using the split() method.

var someString = "string";

var stringToArray = someString.split("");

console.log(stringToArray);

#Output:
["s", "t", "r", "i", "n", "g"]

And now an example using a string with spaces.

var someString = "this is a string with spaces";

var stringToArray = someString.split(" ");

console.log(stringToArray);

#Output:
["this", "is", "a", "string", "with", "spaces"]

When working with different objects in JavaScript, the ability to be able to convert objects into other objects easily can be useful.

One such case is if you want to convert a string into an array in JavaScript.

To convert a string into an array using JavaScript, the easiest way is with the Array.from() method.

Array.from() creates a new array with elements containing each character of the string.

Below is a simple example showing you how to convert a string to an array in JavaScript.

var someString = "string";

var stringToArray = Array.from(someString);

console.log(stringToArray);

#Output:
["s", "t", "r", "i", "n", "g"]

Using split() to Split String by Delimiter into Array Using JavaScript

A very useful function in JavaScript is the split() method.

By default, split() converts a string into an array where each item is the words of the string.

You can also use the split() method to split a string by a different delimiter.

With split(), you can convert a string into an array.

For example, if you wanted to split a string by the spaces, then you can use split() in the following way.

var someString = "This is a string with spaces";

var stringToArray = someString.split(" ");

console.log(stringToArray);

#Output:
["This", "is", "a", "string", "with", "spaces"]

Hopefully this article has been useful for you to learn how to convert a string to array in JavaScript.

Other Articles You'll Also Like:

  • 1.  JavaScript Trunc with Math.trunc() Method
  • 2.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 3.  Using JavaScript to Get a Random Number Between 1 and 10
  • 4.  How to Use JavaScript to Change Text in Span
  • 5.  Check That String Does Not Contain Character in JavaScript
  • 6.  How to Call a JavaScript Function From HTML
  • 7.  Using JavaScript to Get Radio Button Value
  • 8.  Remove All Instances of Value From an Array in JavaScript
  • 9.  Using JavaScript to Remove Apostrophe From a String
  • 10.  JavaScript value – Get the Value from an Input Field

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy