The spread
Syntax
You can use the spread syntax, an Array Initializer introduced in ECMAScript 2015 (ES6) standard:
var arr = [...str];
Examples
function a() {_x000D_
return arguments;_x000D_
}_x000D_
_x000D_
var str = 'Hello World';_x000D_
_x000D_
var arr1 = [...str],_x000D_
arr2 = [...'Hello World'],_x000D_
arr3 = new Array(...str),_x000D_
arr4 = a(...str);_x000D_
_x000D_
console.log(arr1, arr2, arr3, arr4);
_x000D_
The first three result in:
["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]
The last one results in
{0: "H", 1: "e", 2: "l", 3: "l", 4: "o", 5: " ", 6: "W", 7: "o", 8: "r", 9: "l", 10: "d"}
Browser Support
Check the ECMAScript ES6 compatibility table.
Further reading
spread
is also referenced as "splat
" (e.g. in PHP or Ruby or as "scatter
" (e.g. in Python).
Demo