Wednesday, January 8, 2025
HomeProgrammingJavaScript Arrays

JavaScript Arrays

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.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x