Difference Between “{}” and “[]” while Declaring A JavaScript Array?

Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed. Since an array’s length can change at any time, and data can be stored at non-contiguous locations in the array, JavaScript arrays are not guaranteed to be dense; this depends on how the programmer chooses to use them.

Example of  JavaScript Array

Create an Array

let fruits = ['Apple', 'Banana']

console.log(fruits.length)
// 2

Access an Array item using the index position

let first = fruits[0]
// Apple

let last = fruits[fruits.length - 1]
// Banana

 

Difference  Between “{}” and “[]” while Declaring A JavaScript Array?

[] is declaring an array.

{} is declaring an object.

 

An array has all the features of an object with additional features (you can think of an array like a sub-class of an object) where additional methods and capabilities are added in the Array sub-class. In fact, typeof [] === "object" to further show you that an array is an object.

The additional features consist of a magic .length property that keeps track of the number of items in the array and a whole slew of methods for operating on the array such as .push().pop().slice().splice(), etc… You can see a list of array methods here.

An object gives you the ability to associate a property name with a value as in:

Leave a Comment