Creating array of empty strings?
You can get an array defining the size and fill it with some tokens:
const arr = Array(size).fill("");
here's a simpler way using generic protos on Array and String:
"".split.call(Array(1001), ",")
EDIT: There's now even simpler ways, some of which are readable:
Array(1000).fill("");
" ".repeat(999).split(" ");
Update: on newer browsers - use .fill
: Array(1000).fill('')
will create an array of 1000 empty strings.
Yes, there is a way:
var n = 1000;
Array(n).join(".").split("."); // now contains n empty strings.
I'd probably use the loop though, it conveys intent clearer.
function repeat(num,whatTo){
var arr = [];
for(var i=0;i<num;i++){
arr.push(whatTo);
}
return arr;
}
That way, it's perfectly clear what's being done and you can reuse it.