What is an Array?
Defining an Array:
var fruits = ["apple", "banana", "orange"];
Accessing Array Elements:
You can access individual elements in an array by using their index. For example, to access the first element (apple) in the fruits array defined above, you would use:
var firstFruit = fruits; // apple
You can determine the number of elements in an array using the
length property. This property returns the total number of elements present in the array. For example:
var numFruits = fruits.length; // 3
Modifying Array Elements:
fruits = "grape"; // ["apple", "grape", "orange"]
- push(): Adds one or more elements to the end of an array.
- pop(): Removes the last element from an array and returns that element.
- shift(): Removes the first element from an array and returns that element.
- unshift(): Adds one or more elements to the beginning of an array.
- splice(): Changes the contents of an array by removing, replacing, or adding elements.