Transform string into array in JavaScript

November 25, 2021

Transform string into array in JavaScript

In this article we will learn how to transform string into array with JavaScript language and even with a native method, in an easy and uncomplicated way.

Transform string into array capa

Hey you programmer, okay? What about learning something new?

Our focus here is to transform a string into an array

Either because we want to create this set or also when we get some text from the backend, for example, and we want to turn it into an array

With JavaScript it’s very easy to solve this, we have a native method for that matter

| Do you want to specialize in Web Development? Check out our course catalogue.

Native methods are the functions that come within the language, that is, ready to use

See the example:

const texto = "Temos uma string aqui";

const array = texto.split(" ");

console.log(array); // ["Temos", "uma", "string", "aqui"]; 

Using the split method on a string, we get an array back

It is important to remember that the split parameter must be the array separator

By comma, space, dash, in short, the way we want the string to be separated

So it will be split every time split finds the parameter in the string, generating the array


In JavaScript we can easily turn a string into an array

For this we use the join method, we must assign it to a variable and use it in the string

And the parameter passed is the array separator, for example: if we pass a comma the string will be separated into an array for each comma found

Want to learn more about JavaScript resources? Click here!

Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x