Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split strings into an array in javascript?

How to split strings into an array in javascript?
I tried this below

const Vehicles = "Sedan" + "Coupe" + "Minivan"
  const Output = Vehicles.split(",")

   console.log(Output)

and the results was

["SedanCoupeMinivan",]

However I would like the results to instead be this below

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

["Sedan", "Coupe", "Minivan"]

>Solution :

Your original string

const Vehicles = "Sedan" + "Coupe" + "Minivan"

results in "SedanCoupeMinivan" as the value of Vehicles.

Then you try to split that by a comma:

const Output = Vehicles.split(",")

As the orignal string that you tried to split does not contain a single comma, the result you got is quite what I would expect.

You could assemble the original string with commas:

const Vehicles = "Sedan" + "," + "Coupe" + "," + "Minivan"

and the split should work as you expected.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading