Dave's Brain

Browse - Programming Tips - In javaScript when should I use `new Array()` ?

Date: 2017nov17
Language: javaScript
Keywords: create, initialize

Q.  In javaScript when should I use `new Array()` ?

A.  To create an empty array you can do:

let a = new Array(); let a = new Array; let a = []; // BEST
The last one is the most compact so I prefer it. If you want to create an array of known size, there is only one way:
let a = new Array(100);
So this is when to use `new Array`.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.