EN
JavaScript - arrays braces vs brackets
1
answers
0
points
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
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
0 comments
Add comment