Array() vs new Array()
The spec says:
When
Array
is called as a function rather than as a constructor, it creates and initialises a new Array object. Thus the function callArray(…)
is equivalent to the object creation expressionnew Array(…)
with the same arguments.
What’s the difference between Array() and [] while declaring a JavaScript array?
There is a difference, but there is no difference in that example.
Using the more verbose method: new Array()
does have one extra option in the parameters: if you pass a number to the constructor, you will get an array of that length:
x = new Array(5);
alert(x.length); // 5
To illustrate the different ways to create an array:var a = [], // these are the same
b = new Array(), // a and b are arrays with length 0
c = ['foo', 'bar'], // these are the same
d = new Array('foo', 'bar'), // c and d are arrays with 2 strings
// these are different:
e = [3] // e.length == 1, e[0] == 3
f = new Array(3), // f.length == 3, f[0] == undefined
;
Another difference is that when using new Array()
you're able to set the size of the array, which affects the stack size. This can be useful if you're getting stack overflows (Performance of Array.push vs Array.unshift) which is what happens when the size of the array exceeds the size of the stack, and it has to be re-created. So there can actually, depending on the use case, be a performance increase when using new Array()
because you can prevent the overflow from happening.As pointed out in this answer, new Array(5)
will not actually add five undefined
items to the array. It simply adds space for five items. Be aware that using Array
this way makes it difficult to rely on array.length
for calculations.
Difference between new Array(..) and [..] in JavaScript?
Yes, for that case they are the same.
There is a difference if you have only one item, and it's numeric. This will create an array with a single item:
var myNumbers = [42];
but this will create an array with the length 42:var myNumbers = new Array(42);
What's the difference between Array(1) and new Array(1) in JavaScript?
With Array
, both are equivalent. The new
is injected when it's called as a function:
15.4.1 TheFrom ECMA-262, 3th Edition (with similar in 5th Edition). See also 22.1.1 The Array Constructor in ECMA-262 ECMAScript 2020 specification (11th Edition).Array
Constructor Called as a FunctionWhen
Array
is called as a function rather than as a constructor, it creates and initialises a new Array object. Thus the function callArray(…)
is equivalent to the object creation expressionnew Array(…)
with the same arguments.
Array.of() vs new Array()
Array#of
documentation
The difference between
Array.of()
and theArray
constructor is in the
handling of integer arguments:Array.of(7)
creates an array with a
single element, 7, whereasArray(7)
creates an empty array with a
length property of 7 (Note: this implies an array of 7 empty slots,
not slots with actual undefined values).
What is the different vs array.push() and array = [...array, newItem]?
Push: Use push when you want to add data in the existing array and don't want to cra
When you use the push method, you are adding the element to the existing one i.e not creating a new array.
The push() method adds one or more elements to the end of an array and
returns the new length of the array. - MDN
const arr = [1, 2, 3, 4, 5];
const returnValue = arr.push(6);
console.log(arr);
console.log(returnValue)
In C++, what is the difference between new and new[] for array allocations
Some clever implementations of malloc don't actually keep track of the size per allocation (by clever use of rounding up), thus it have extremely low space overhead. They'll allocate a large block of memory, and anytime a dev allocates <64 bytes, it'll just use the next 64 bytes of this block, and mark a single bit elsewhere that tracks that this block of 64 bytes is now in use. Even if the user only wants to allocate 1 byte, it hands out a whole block. This means each allocation has only a single bit overhead, so every 8 allocations has a shared byte of overhead. Practically nothing. (There are far smarter strategies than this, this is just a simplified example)
new
and delete
can share this super-low-overhead implementation, because delete
knows to always destroy one object, regardless of the amount of space it actually has. This is again, super fast, and has low space overhead.
delete[]
can't do that because it has to know exactly how many destructors to call. So it has to keep track of how many items are in the array, up to std::size_t
, which means ~4 bytes have to be added to every new[]
. If the data requires an alignment >4, then each allocation also has to have wasted padding bytes between the count and the first item in the array. And delete[]
therefore has to know how to look past the padding, find the count, so it knows exactly how many objects to destroy. This takes both time and more space, for every allocation.
C++ gives you the choice between "always works, but slower and bigger", and "only works for one item, but faster and smaller", so the program can run as fast as possible.
PHP: What's the difference between initializing an array with new vs without it?
You don't instantiate an array in PHP using:
$foo=new array(); // error in PHP
That's for Javascript:foo=new Array(); // no error in Javascript
In PHP, new
is used only for instantiating objects.
Related Topics
Unexpected Token Illegal in Webkit
Differencebetween React Native and React
JavaScript Add Method to Object
Prevent JavaScript Keydown Event from Being Handled Multiple Times While Held Down
How to Auto-Reload Files in Node.Js
How to Pass a Flag to Gulp to Have It Run Tasks in Different Ways
Node.Js Piping the Same Readable Stream into Multiple (Writable) Targets
Differencebetween Decodeuricomponent and Decodeuri
Overriding Interface Property Type Defined in Typescript D.Ts File
JavaScript .Replace Only Replaces First Match
Adding New Data to Firebase Users
How to Parse a Date in Format "Yyyymmdd" in JavaScript
How to Update a Specific Index from the Array in Firestore
Render Object Properties in React
How to Use Jquery to Style /Parts/ of All Instances of a Specific Word