Introduction to Javascript Arrays
Introduction to Javascript Arrays
What is an Array?
• An array is a collection of multiple values stored inside a single
variable.
• Arrays can store values of different types, but usually store similar
types (like names, numbers, etc.).
• Each value in an array is identified by an index, starting from 0
(zero).
Example:
let cars = ["Volvo", "BMW", "Audi", "Benz"];
• cars[0] → "Volvo"
• cars[1] → "BMW"
• cars[2] → "Audi"
• cars[3] → "Benz"
Built-in Array Methods:
JavaScript provides methods to perform common array operations easily.
push() - Adds a new element at the end of the array.
pop() - Removes the last element from the array.
unshift() - Adds a new element at the beginning of the array.
shift() - Removes the first element of the array.
indexOf() - Returns the first index at which a given element is found.
- Returns -1 if the element is not found.
length - Returns the total number of elements in the array.
sort() - method sorts the elements alphabetically (A-Z order).