What is an Array?
An array is a special variable in JavaScript that can hold multiple values in a single variable. You can think of an array as a list of values.
Creating an Array
You can create an array by enclosing values in square brackets (`[]`), separated by commas.
let fruits = ['apple', 'banana', 'cherry'];
Accessing Array Elements
You can access the elements in an array by using an index. The index starts from 0.
console.log(fruits[0]); // Output: apple
Common Array Methods
- push(): Adds an element to the end of the array.
- pop(): Removes the last element from the array.
- shift(): Removes the first element from the array.
- unshift(): Adds an element to the beginning of the array.
- forEach(): Executes a function on each element of the array.
Interactive Example
Click the button to add a fruit to the list: