Arrays are a fundamental concept in programming. They allow you to store multiple values in a single variable. Here are some key points to understand:
- An array is a data structure that holds a collection of elements. It is created using square brackets, and elements are separated by commas.
let fruits = ["apple", "banana", "orange"]; // contains 3 elements(fruits).
- Arrays can store different types of data, such as numbers, strings, or even other arrays.
let mixed = ["Hi", 1, true, [1, 2, 3]];
- You can access individual elements in an array using square brackets notation and the index.
let fruits = ["apple", "banana", "orange"]; console.log(fruits[0]); // apple
- Each element in an array has an index, which represents its position. Indexing starts from 0.
let fruits = ["apple", "banana", "orange"]; console.log(fruits[0]); // apple console.log(fruits[1]); // banana console.log(fruits[2]); // orange
- Arrays are mutable data structures in JavaScript. This means that you can modify the elements of an array after its creation.
JavaScript provides various methods and operations to manipulate arrays, such as adding or removing elements, sorting, filtering, and more. These operations can modify the contents of an array, making it mutable.
let myArray = [1, 2, 3, 4, 5]; myArray[2] = 10; // Modifying the element at index 2 console.log(myArray); // Output: [1, 2, 10, 4, 5]
Arrays in JavaScript are special objects designed to hold and organize multiple values in a specific order. They use numerical indices and have a length property. Arrays allow you to add, remove, and modify elements. They are efficient for working with collections of data.