An array is a collection of multiple values stored in a single variable.
It is an Object type.
List of values known as element.
The first element is at index 0, the second at index 1, and so on.
Arrays can grow or shrink as elements are added or removed.
Arrays can store elements of different data types. So it is Heterogeneous.
Why use Array?
Instead of declaring dozens of separate variables for related items (like car1, car2, etc.), they can be grouped into one array.
Unlike in many other languages, JavaScript arrays are resizable and can hold a mix of different data types.
Arrays make it easy to loop through thousands of items to perform bulk operations.
Creating an Array:
constfruits=["apple","orange","banan"]//You can also create an empty array, and provide elements later:constfruits=[];fruits[0]="Apple";fruits[1]="Orange";fruits[2]="Banana";//The following example also creates an Array, and assigns values to it:constfruits=newArray("Apple","Orange","Banana");
Top comments (0)