Arrays in JavaScript are used to store multiple values in a single variable. They can hold elements of any data type and are zero-indexed.
Common Operations:
- Create an Array:
let fruits = ["Apple", "Banana", "Cherry"];
- Access Elements:
console.log(fruits[0]); // Apple
- Add/Remove Elements:
fruits.push("Mango"); // Add at end
fruits.pop(); // Remove from end
fruits.unshift("Grapes"); // Add at start
fruits.shift(); // Remove from start
- Iterate:
fruits.forEach(fruit => console.log(fruit));
- Length:
console.log(fruits.length); // Number of elements
Advanced:
- Sort:
fruits.sort();
- Filter/Map/Reduce:
let result = fruits.filter(fruit => fruit.startsWith("A"));
Arrays are versatile and widely used in JavaScript for data manipulation and storage.