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

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.