Skip to main content

Arrays

Arrays are one of the most fundamental data structures in programming. They are used to store multiple values in a single variable, allowing for efficient data manipulation and access. This guide will cover how to declare, initialize, and work with arrays in JavaScript, Java, Python, and C++.

What is an Array?​

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together to make it easier to manage them. Arrays can be indexed, modified, and iterated over to perform various operations.

Arrays in Different Languages​

JavaScript Arrays Overview​

In JavaScript, arrays are dynamic, which means they can change size and hold mixed data types.

Declaration and Initialization​

JavaScript Array Example
// Declaration
let fruits = ["Apple", "Banana", "Cherry"];

// Accessing elements
console.log(fruits[0]); // Output: Apple

// Modifying an array
fruits[1] = "Mango";
console.log(fruits); // Output: ["Apple", "Mango", "Cherry"]

Common Array Methods​

  • push(): Adds an element to the end.
  • pop(): Removes the last element.
  • shift(): Removes the first element.
  • unshift(): Adds an element to the beginning.
JavaScript Array Methods Example
fruits.push("Orange");
console.log(fruits); // Output: ["Apple", "Mango", "Cherry", "Orange"]

fruits.pop();
console.log(fruits); // Output: ["Apple", "Mango", "Cherry"]

Visualizing Arrays with Mermaid​

Below is a simple Mermaid diagram to visualize how an array is structured in memory.


This representation shows how each element in an array can be accessed by its index.


Feedback and Support