Categories
JavaScript Answers

How to convert a string array to an array in JavaScript?

Spread the love

Sometimes, we want to convert a string array to an array in JavaScript.

In this article, we’ll look at how to convert a string array to an array in JavaScript.

How to convert a string array to an array in JavaScript?

To convert a string array to an array in JavaScript, we can use the JSON.parse method.

For instance, we write:

const services = '["service1", "service2", "service3"]'
const s = JSON.parse(services)
console.log(s)

We call JSON.parse with services to parse it into a JavaScript array.

Therefore, s is ['service1', 'service2', 'service3'].

Conclusion

To convert a string array to an array in JavaScript, we can use the JSON.parse method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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