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
["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.