Lesson 13
Arrays Basics
Arrays are used to store multiple values in a single variable. Each value in an array is called an element.
- What is an Array?
- Creating Arrays
- Accessing Elements
- Array Length
- Common Operations
- Iterating Arrays
An array is an ordered collection of values. Elements are stored using a zero-based index.
Ordered
Elements have a specific position (index).
Zero-based Indexing
The first element is at index 0.
Holds Any Type
An array can store elements of any data type.
Example
1let numbers = [10, 20, 30, 40, 50];2let fruits = ["Apple", "Banana", "Mango"];3let mixed = [1, "Vivek", true, null];arr = [10, 20, 30, 40, 50]| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Value | 10 | 20 | 30 | 40 | 50 |
First element: arr[0] → 10
Last element: arr[arr.length - 1] → 50
Accessing elements using index.
JavaScript
Output
Your output will appear here...
Use arr[index] to access an element at a specific position.
- // First element
arr[0] - // Last element
arr[arr.length - 1] - // Total elements
arr.length - // Add at end
arr.push(x) - // Remove from end
arr.pop()
Loop through each element.
JavaScript
Output
Your output will appear here...
Arrays in JavaScript are dynamic. You can add, remove, and update elements even after creating the array.
