+ 2
There are two syntaxes for creating an array
const arr1=[1,2,3,3] const arr2=new Array(1,2,3,3) But what is the difference between these two syntaxes ?
3 Answers
+ 4
Quoted from:
https://www.w3docs.com/snippets/javascript/how-to-declare-and-initialize-an-array-in-javascript.html
"Using Array literal notation:
If you put a number in the square brackets [], it will return the number. While using new Array() if you pass a number to the constructor, you will get an array of that length.
You call the Array() constructor with two or more arguments, the arguments will create the array elements. If you only invoke one argument, the argument initializes the length of the new array; the new array's elements are not initialized (read: undefined)."
const arr1 = [5]; // creates 1 value (5) in <arr1>
const arr2 = new Array(5); // creates 5 undefined values in <arr2>
+ 2
Check out this post
https://stackoverflow.com/questions/8205691/array-vs-new-array
+ 1
Ipang ,
Thank you ! I understand đ