🎂Arrays

In JavaScript, an array is a data structure that stores a collection of elements. Elements in an array can be of any data type, such as numbers, strings, objects, or even other arrays.

Arrays are created using the literal notation array, square brackets [] with elements separated by commas. For example:

let myArray = [1, 2, 3, 4, 5];

OR

let myArray = ["apple", "banana", "orange"];

Elements in an array can be accessed by their index, which is the position of the element in the array starting from 0. For example:

console.log(myArray[0]); // Output: 1
console.log(myArray[1]); // Output: 2

JavaScript arrays also have built-in methods that can be used to manipulate the elements, such as push() to add an element to the end of the array, pop() to remove the last element, shift() to remove the first element, unshift() to add an element to the beginning of the array, and others.

JavaScript also has several array prototype methods, such as filter() and map() which are used for iterating over the array and applying a function to each element and returning the new array.

In short, Array is a data structure that stores multiple elements of the same or different types in a single variable. It is a powerful tool that allows you to organize and manipulate data in a convenient and efficient way.

Last updated