Introduction
Welcome to Day 6 of your JavaScript journey! 🌟 Yesterday, we explored functions. Today, we will dive into arrays, one of the most important data structures in JavaScript. Arrays allow you to store multiple values in a single variable, making it easier to manage and manipulate collections of data. Let’s get started! 🎉
please subscribe to my YouTube channel to support my channel and get more web development tutorials.
What is an Array? 📚
An array is a special type of object that can hold an ordered list of values. Each value (or element) in an array has a numeric index, starting from 0.
Example:
console.log(fruits[0]); // Output: Apple 🍎
Creating Arrays 🌱
You can create arrays in multiple ways:
1. Using Array Literals
2. Using the Array Constructor
Accessing Array Elements 🔍
You can access elements in an array using their index:
Example:
console.log(colors[1]); // Output: Green 🍏
Common Array Methods 🛠️
JavaScript provides various methods to manipulate arrays:
1. push()
Adds one or more elements to the end of an array.
animals.push(“Elephant“);
console.log(animals); // Output: [“Dog”, “Cat”, “Elephant”] 🐘
2. pop()
Removes the last element from an array.
animals.pop();
console.log(animals); // Output: [“Dog”, “Cat”] 🐶🐱
3. shift()
Removes the first element from an array.
birds.shift();
console.log(birds); // Output: [“Sparrow”, “Peacock”] 🦜
4. unshift()
Adds one or more elements to the beginning of an array.
birds.unshift(“Parrot“);
console.log(birds); // Output: [“Parrot”, “Sparrow”, “Peacock”] 🦜
5. forEach()
Executes a provided function once for each array element.
cars.forEach(function(car) {
console.log(car);
});
// Output:
// Tesla 🚗
// BMW 🚙
// Audi 🚘
6. map()
Creates a new array populated with the results of calling a provided function on every element in the calling array.
let squares = numbers.map(function(number) {
return number * number;
});
console.log(squares); // Output: [1, 4, 9, 16, 25] 🔢
7. filter()
Creates a new array with all elements that pass the test implemented by the provided function.
let evenNumbers = numbers.filter(function(number) {
return number % 2 === 0;
});
console.log(evenNumbers); // Output: [2, 4] ⚖️
8. reduce()
Executes a reducer function on each element of the array, resulting in a single output value.
let sum = numbers.reduce(function(total, number) {
return total + number;
}, 0);
console.log(sum); // Output: 15 ➕
Practical Examples 🧩
Example 1: Find the maximum number in an array
let max = numbers.reduce(function(a, b) {
return Math.max(a, b);
});
console.log(“Max number:“, max); // Output: Max number: 50 🔝
Example 2: Create a new array with elements in uppercase
let upperCaseFruits = fruits.map(function(fruit) {
return fruit.toUpperCase();
});
console.log(upperCaseFruits); // Output: [“APPLE”, “BANANA”, “CHERRY”] 🍒
Practice Activities 💪
1. Practice Code:
Create arrays using literals and the Array constructor.
Access and manipulate array elements using various methods.
2. Mini Project:
Create a simple script that takes a list of student names and returns the names in alphabetical order.
Example:
students.sort();
console.log(“Sorted names:“, students);
// Output: Sorted names: [“Alice”, “Bob”, “Charlie”] 📚
Summary 📋
Today, we explored arrays in JavaScript. We learned how to create arrays, access their elements, and use common array methods to manipulate data. Arrays are a fundamental part of JavaScript, and mastering them is crucial for effective programming.
Stay tuned for Day 7, where we’ll dive into objects and their properties in JavaScript! 🏆
Feel free to leave your comments or questions below. If you found this guide helpful, please share it with your peers and follow me for more web development tutorials. Happy coding!
Follow and Subscribe:
Website: Dipak Ahirav
Email: dipaksahirav@gmail.com
Instagram: devdivewithdipak
YouTube: devDive with Dipak
LinkedIn: Dipak Ahirav