An array, is a data structure consisting of a collection of elements, each identified by at least one array index or key. It is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
- The array literal, which uses square brackets.
- By creating instance of Array directly (using new keyword).
- The array constructor, which uses the new keyword.
Using Array Literal
By creating instance of Array directly
Using Array Constructor
Array Element Type
Array elements can be any type, including number, string, Boolean , null, undefined, object, function, regular expression and other arrays.
Access the Elements of an Array
The length Property
When you run the above program, it will return 5.
Looping Array Elements
Another way you can use for loop...
Add to the end of an Array
The push() method will add an element to the end of an array, while its twin function, the pop() method, will remove an element from the end of the array.
Remove from the end of an Array
Add item(s) to the front of an Array
Remove item(s) from the front of an Array
Modifying Items in Arrays
Array elements are accessed using their index number . Using this index number you can modify an item in an array.
Find the index of an item in the Array
The above program return 2.
The above code retuen "false", because "Friday" is not contains the array "days".
Arrays Undefined Values
Arrays are automatically growing and dynamically sized to accommodate any type of data that is added to them. So, when you add or remove elements from an array, the length of the array will change as needed. When you declare an Array with constructor , each slot will be set to "undefined".
Above program return 5.
The toString() method returns a string representation of an object.