Look at the code:
var string = "0,1"; var array = [string]; alert(array);
In this case
0,1. If it where an array, it would show
0. And if
alert(array) is called, it should pop-up
For simple array members like that, you can use
var array = JSON.parse("[" + string + "]");
This gives you an Array of numbers.
If you use
.split(), you’ll end up with an Array of strings.
Just be aware that
JSON.parse will limit you to the supported data types. If you need values like
undefined or functions, you’d need to use
If you want to use
.split(), but you also want an Array of Numbers, you could use
Array.prototype.map, though you’d need to shim it for IE8 and lower or just write a traditional loop.
var array = string.split(",").map(Number);
Answered By – I Hate Lazy
Answer Checked By – Marie Seifert (BugsFixing Admin)