Categories
JavaScript Answers

How to convert a string to an object with JavaScript?

Spread the love

Sometimes, we want to convert a string to an object with JavaScript.

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

How to convert a string to an object with JavaScript?

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

For instance, we write:

const str = '{"a":"www"}';
const obj = JSON.parse(str);
console.log(obj)

to call JSON.parse with str to return an object from the str JSON string.

Therefore, obj is {a: 'www'}.

Conclusion

To convert a string to an object with 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 *