Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  139] [ 6]  / answers: 1 / hits: 58580  / 10 Years ago, thu, january 8, 2015, 12:00:00

In Javascript, why is


var myArray = new Array(3);

different from:


var otherArray = [null, null, null];

?


Obs: (myArray == otherArray) returns false.


And also, how can I get a variable like otherArray, which is an array full of 'nulls`, but with whatever size i'd like?


Obs


[undefined, undefined, undefined] 

is also not equal to myArray.


More From » arrays

 Answers
5

With EcmaScript 6 (ES2105), creating an array containing five nulls is as easy as this:


const arr = new Array(5).fill(null);

MDN Reference


[#68280] Monday, January 5, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaredsages

Total Points: 273
Total Questions: 97
Total Answers: 105

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
jaredsages questions
;