JavaScript Arrays
-
Ordered Collection: An array is an ordered collection of values (elements). These values can be of any data type, including numbers, strings, objects, and even other arrays.
-
Indexing: Each element in an array has an index, which is its position within the array. The index starts from 0 (the first element).
-
Creating Arrays:
const myArray = [1, 2, 3, "hello", true];const emptyArray = [];
-
Accessing Elements:
myArray[0] // Accesses the first element (1)myArray[myArray.length - 1] // Accesses the last element
-
Array Methods:
push(): Adds elements to the end of the array.pop(): Removes the last element from the array.shift(): Removes the first element from the array.unshift(): Adds elements to the beginning of the array.slice(): Creates a shallow copy of a portion of the array.splice(): Adds/removes elements from an array at a specific position.join(): Converts the elements of an array into a string.concat(): Creates a new array by concatenating existing arrays.forEach(),map(),filter(),reduce(): Higher-order functions for iterating and manipulating arrays.