To create an array literal, you put a list of values between square brackets:
Like regular expression literals, array literals provide compilation of the array when the script is loaded. Therefore, they can be better for performance.
You’ll use these most of the time.
You can also create an array by passing your values into the
Array() constructor function:
There’s really not much point in doing this, though. This method is worse for performance and longer to write.
The nice thing about the
Array() constructor is that you can pass in a number. This creates an array with that number of empty slots:
Here’s an important note from the MDN Web Docs:
If the only argument passed to the
lengthproperty set to that number (Note: this implies an array of
arrayLengthempty slots, not slots with actual
undefinedvalues). If the argument is any other number, a
RangeErrorexception is thrown.
Array literals are better for performance, so you should use these most of the time. If you need to create an array with a number of empty slots, you can use the
If you have questions, feedback, or any other suggestions, please do email me. I'd love to hear from you!