KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I see four five options to initialize an array to a specific length in JS, (the last one is a stretch, I know): var a = []; a.length = 5; var a = Array(5); var a = []; a[4] = undefined; var a = new Array(5); function makeArrayToLength(length, default){ var a = [], i = 0; for(; i < length; i++){ a[i] = default; } return a; } I definitely want to (and do) use the literal whenever possible, but I'm in a situation where the defining aspect of a particular array is its length, so I'm leaning towards Array(5). Is the first example equivalent to the second in terms of end result? I realize it's not equivalent in the execution.
Tags (comma-separated)
Save Edits
Cancel