Featured
- Get link
- X
- Other Apps
๐งฉ Day 7 — Arrays & Array Methods in JavaScript
๐งฉ Day 7 — Arrays & Array Methods in JavaScript
Welcome to Day 7 ๐ — Today we’ll dive into one of the most essential data structures in JavaScript — the Array. Arrays allow you to store multiple values in a single variable, making it easy to handle collections of data like numbers, names, or products.
๐ฆ 1️⃣ What is an Array?
An array is a special variable that can hold multiple values at once.
Instead of creating several variables, you can group values inside square brackets [].
let fruits = ["Apple", "Banana", "Cherry"]; console.log(fruits); // ["Apple", "Banana", "Cherry"]
๐ 2️⃣ Accessing Elements
Each element in an array has an index (position), starting from 0.
console.log(fruits[0]); // Apple console.log(fruits[2]); // Cherry
➕ 3️⃣ Modifying Arrays
You can change values using their index or add new ones dynamically.
fruits[1] = "Mango"; // Change Banana to Mango
fruits.push("Grapes"); // Add to end
fruits.unshift("Orange"); // Add to start
console.log(fruits);
๐งน 4️⃣ Removing Elements
There are multiple ways to remove elements from arrays:
fruits.pop(); // Removes last fruits.shift(); // Removes first fruits.splice(1, 1); // Removes one element at index 1
⚙️ 5️⃣ Common Array Methods
Let’s explore the most used methods to process arrays:
.length— Returns total elements.indexOf()— Finds index of a value.includes()— Checks if value exists.concat()— Merges two arrays
let colors = ["Red", "Blue", "Green"];
console.log(colors.length); // 3
console.log(colors.indexOf("Blue")); // 1
console.log(colors.includes("Yellow")); // false
๐งฉ 6️⃣ Array Iteration (map, filter, forEach)
forEach()
Executes a function for each array element.
colors.forEach((color) => console.log(color));
map()
Creates a new array with transformed values.
let upperColors = colors.map((c) => c.toUpperCase()); console.log(upperColors);
filter()
Returns a new array with only elements that match a condition.
let numbers = [10, 20, 30, 40]; let above20 = numbers.filter((num) => num > 20); console.log(above20); // [30, 40]
๐ง 7️⃣ Practice Task
- Create an array of your top 5 favorite movies.
- Add and remove a movie using
push()andpop(). - Use
map()to make them all uppercase. - Filter movies with more than 5 letters.
๐ฏ Summary
Arrays are essential for grouping and processing data efficiently. Tomorrow, we’ll explore **Strings and String Methods**, another core part of JavaScript data handling.
Popular Posts
๐ Day 27: Bootstrap Mini Project – Responsive Portfolio Page
- Get link
- X
- Other Apps
๐จ Day 2: Headings, Paragraphs & Text Formatting
- Get link
- X
- Other Apps
Comments
Post a Comment