Languages

JavaScript - arrays braces vs brackets

0 points
Asked by:
Palpys
764

Can you tell me what is the difference between braces and brackets in array definition?

Example:

var array1 = {};
var array2 = [];
var array3 = new Array();
1 answer
0 points
Answered by:
Palpys
764

The first one doesn't create an array, it creates a new empty object. The second and third create a new array and they are equivalent.

Here's a simple type check:

// ONLINE-RUNNER:browser;

var array1 = {};          // creates new empty object
var array2 = [];          // creates new empty array
var array3 = new Array(); // creates new empty array


// Check if array:

console.log(Array.isArray(array1)); // false
console.log(Array.isArray(array2)); // true
console.log(Array.isArray(array3)); // true

 

See also

  1. JavaScript - check if variable is array type

0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join