JavaScript Tutorial in Hindi #17 | JavaScript Array Basics (2025)
✅ 1. What is an Array?
An array is a special variable that can hold multiple values in a single variable.
js
CopyEdit
let fruits = ["Apple", "Banana", "Mango"];
✅ 2. Create Array and Accessing Elements
Creating an Array:
js
CopyEdit
let colors = ["Red", "Green", "Blue"];
Accessing Elements (Index starts from 0):
js
CopyEdit
console.log(colors[0]); // "Red"
console.log(colors[2]); // "Blue"
✅ 3. Changing and Adding Array Elements
Change existing value:
js
CopyEdit
colors[1] = "Yellow"; // changes "Green" to "Yellow"
Add new value:
js
CopyEdit
colors[3] = "Purple"; // adds "Purple" at index 3
✅ 4. Basic Array Methods
👉 .push()
Adds an element to the end of the array.
js
CopyEdit
let nums = [1, 2, 3];
nums.push(4);
console.log(nums); // [1, 2, 3, 4]
👉 .pop()
Removes the last element from the array.
js
CopyEdit
nums.pop();
console.log(nums); // [1, 2, 3]
👉 .shift()
Removes the first element from the array.
js
CopyEdit
let items = ["Pen", "Pencil", "Eraser"];
items.shift();
console.log(items); // ["Pencil", "Eraser"]
👉 .unshift()
Adds an element to the beginning of the array.
js
CopyEdit
items.unshift("Marker");
console.log(items); // ["Marker", "Pencil", "Eraser"]
💡 Summary Table:
MethodActionPosition Affected | ||
push() | Add to end | Last |
pop() | Remove from end | Last |
shift() | Remove from start | First |
unshift() | Add to start | First |