Day 6: Mastering Arrays in JavaScript π
Dipak Ahirav
Posted on June 16, 2024
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:
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // Output: Apple π
Creating Arrays π±
You can create arrays in multiple ways:
1. Using Array Literals
let numbers = [1, 2, 3, 4, 5];
2. Using the Array
Constructor
let numbers = new Array(1, 2, 3, 4, 5);
Accessing Array Elements π
You can access elements in an array using their index:
Example:
let colors = ["Red", "Green", "Blue"];
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.
let animals = ["Dog", "Cat"];
animals.push("Elephant");
console.log(animals); // Output: ["Dog", "Cat", "Elephant"] π
2. pop()
Removes the last element from an array.
let animals = ["Dog", "Cat", "Elephant"];
animals.pop();
console.log(animals); // Output: ["Dog", "Cat"] πΆπ±
3. shift()
Removes the first element from an array.
let birds = ["Parrot", "Sparrow", "Peacock"];
birds.shift();
console.log(birds); // Output: ["Sparrow", "Peacock"] π¦
4. unshift()
Adds one or more elements to the beginning of an array.
let birds = ["Sparrow", "Peacock"];
birds.unshift("Parrot");
console.log(birds); // Output: ["Parrot", "Sparrow", "Peacock"] π¦
5. forEach()
Executes a provided function once for each array element.
let cars = ["Tesla", "BMW", "Audi"];
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 numbers = [1, 2, 3, 4, 5];
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 numbers = [1, 2, 3, 4, 5];
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 numbers = [1, 2, 3, 4, 5];
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 numbers = [10, 20, 30, 40, 50];
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 fruits = ["apple", "banana", "cherry"];
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:
let students = ["Charlie", "Alice", "Bob"];
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.
Series Index
Part | Title | Link |
---|---|---|
1 | Day 1: Getting Started with JavaScript | Read Part 1 |
2 | Day 2: Understanding Variables and Data Types in JavaScript | Read Part 2 |
3 | Day 3: Mastering Operators and Expressions in JavaScript | Read Part 3 |
4 | Day 4: Control Structures in JavaScript | Read Part 4 |
5 | Day 5: Understanding Functions in JavaScript | Read Part 5 |
6 | Day 6: Mastering Arrays in JavaScript π | Read Part 6 |
7 | Day 7: Understanding Objects in JavaScript π | Read Part 7 |
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
Posted on June 16, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.