JavaScript Arrays
An array is a group of memory locations that all have the same name and normally are of the same type(although this attribute is not required in JavaScript).
-
Array is a collection of same and different data types
-
Each data in array called element
-
Each elements has a numeric position, known as its index/indices, in the array
-
The index number starts from Zero
-
In JavaScript, Array is also an object. The typeof operator will return the same
-
Array object has length property which returns the total number of elements
-
An element may be referred to by giving the name of the array followed indexof the element in square brackets ([ ]).
Arrays Construction
-
The first element in every array is the zeroth element.
-
The element of the array c is referred to c[i-0].
-
Array names follow the same conventions as other identifiers
-
A subscripted array name
- Can be used on the left side of an assignment to place a new value into an array element.
- Can be used on the right side of an assignment operation to use its value
Declaring and allocating array
-
JavaScript arrays are Array objects
-
Creating new objects using the new operator is known as creating an instance or instantiating an object
-
Operator new is known as the dynamic memory allocation operator.
Example1- Program to creating an array stores fruits:
Output
Creating an Array
Using an array literal is the easiest way to create a JavaScript Array.
The Syntax of the Array is as follows:
const array_name = [item1, item2, ...];
Example2-Creating Array with help of Using the JavaScript Keyword new:
Output