JavaScript Arrays
JavaScript Arrays
An array in JavaScript is a special variable that can store multiple values in a single variable. Values inside an array are stored in a list format and separated by commas.
✔ Creating an Array
1. Using Square Brackets (Most common)
2. Using new Array() (Less common)
✔ Accessing Array Elements
Array index starts from 0.
✔ Changing Array Elements
✔ Array Properties
| Property | Example | Result |
|---|---|---|
length |
fruits.length |
Number of items in array |
✔ Common Array Methods
| Method | Action |
|---|---|
push() |
Add item to end |
pop() |
Remove last item |
unshift() |
Add item to start |
shift() |
Remove first item |
sort() |
Sort values |
reverse() |
Reverse order |
Example:
✔ Looping Through an Array
for loop:
forEach() method:
✔ Array of Objects
✔ Checking if Value is an Array
Summary
| Feature | Description |
|---|---|
| Store multiple values | In a single variable |
| Zero-based index | First item = index 0 |
| Built-in methods | push, pop, shift, sort, etc. |
| Easy looping | for or forEach |
Arrays are one of the most important parts of JavaScript and widely used in web development, especially in frameworks like React, Angular, and Vue.
