0

I have a variable var s = '["hi","hello","What\'s new"]'; in string format. How do i get the values hi hello What's new separately.
Also the values in the variable s is dynamic, some times it maybe var s = '["wow","okay"]'; or any values.
Can someone help me with this?

3
  • what do you mean by separately? Commented Jul 24, 2021 at 6:34
  • 2
    Use JSON.parse() to get an array from s, then use a for loop to iterate it and get the items - your question looks similar to this question: How to split string which is in the form an array using JavaScript? Commented Jul 24, 2021 at 6:34
  • First of all, there is an error in this code var s = '["hi","hello","What's new"]' you need to write var s = '["hi","hello","What\'s new"]' Commented Jul 24, 2021 at 6:40

1 Answer 1

1

First of all, there is an error in this code var s = '["hi","hello","What's new"]' you need to write var s = '["hi","hello","What\'s new"]' otherwise it will give you this error:

Uncaught SyntaxError: Unexpected identifier

If you are looking for the output like this:

["hi","hello","What's new"]

Then this is the solution:

var s = '["hi","hello","What\'s new"]'
console.log(JSON.parse(s))

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.