There are two syntaxes for creating an array | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 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 ?

6th Sep 2021, 7:39 AM
Ankita Malik
Ankita Malik - avatar
3 ответов
+ 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>
6th Sep 2021, 11:18 AM
Ipang
6th Sep 2021, 9:49 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Ipang , Thank you ! I understand 😃
6th Sep 2021, 12:29 PM
Ankita Malik
Ankita Malik - avatar