An array is a special variable in JavaScript that allows you to store multiple values in a single variable. Arrays are ordered lists, where each value (element) is stored at an index starting from 0.
How to Create an Array:
// Example of an array
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits); // Output: ["Apple", "Banana", "Cherry"]
Accessing Elements in an Array: You can access elements in an array using their index:
console.log(fruits[0]); // Output: "Apple"
console.log(fruits[2]); // Output: "Cherry"
Modifying Elements in an Array:
fruits[1] = "Blueberry";
console.log(fruits); // Output: ["Apple", "Blueberry", "Cherry"]
Questions & Answers:
JavaScript provides many built-in methods for manipulating arrays. Here are a few essential ones:
fruits.push("Mango");
console.log(fruits); // Output: ["Apple", "Blueberry", "Cherry", "Mango"]
fruits.unshift("Grape");
console.log(fruits); // Output: ["Grape", "Apple", "Blueberry", "Cherry", "Mango"]
fruits.pop();
console.log(fruits); // Output: ["Grape", "Apple", "Blueberry", "Cherry"]
fruits.shift();
console.log(fruits); // Output: ["Apple", "Blueberry", "Cherry"]
console.log(fruits.length); // Output: 3
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]); // Prints each fruit
}
Questions & Answers:
An object in JavaScript is a collection of key-value pairs. Objects allow you to store and organize related data together, making them ideal for representing real-world entities like a person or a product.
Creating an Object:
let person = {
firstName: "John",
lastName: "Doe",
age: 30,
isStudent: false
};
console.log(person);
Accessing Object Properties:
console.log(person.firstName); // Output: "John"
console.log(person["lastName"]); // Output: "Doe"
Modifying Object Properties:
person.age = 31;
console.log(person.age); // Output: 31
Adding New Properties:
person.country = "Canada";
console.log(person.country); // Output: "Canada"
Questions & Answers:
Arrays and objects often work together. For example, you might have an array of objects, where each object represents an item.
Example: Array of Objects
let students = [
{ name: "Alice", age: 20 },
{ name: "Bob", age: 22 },
{ name: "Charlie", age: 19 }
];
console.log(students[0].name); // Output: "Alice"
console.log(students[1].age); // Output: 22
Looping Through an Array of Objects:
for (let i = 0; i < students.length; i++) {
console.log(`${students[i].name} is ${students[i].age} years old.`);
}
// Output:
// Alice is 20 years old.
// Bob is 22 years old.
// Charlie is 19 years old.
Questions & Answers:
Objective:
Create a simple to-do list manager using arrays and objects.
Code Example:
let toDoList = [];
// Add a task
function addTask(task) {
toDoList.push({ task: task, completed: false });
console.log(`Added: "${task}"`);
}
// Mark a task as completed
function completeTask(index) {
if (toDoList[index]) {
toDoList[index].completed = true;
console.log(`Task "${toDoList[index].task}" marked as completed.`);
} else {
console.log("Invalid task index.");
}
}
// Show all tasks
function showTasks() {
console.log("To-Do List:");
for (let i = 0; i < toDoList.length; i++) {
let status = toDoList[i].completed ? "✓" : "✗";
console.log(`${i + 1}. [${status}] ${toDoList[i].task}`);
}
}
// Example Usage
addTask("Learn JavaScript");
addTask("Build a to-do app");
showTasks();
completeTask(0);
showTasks();
Questions for Practice:
Recap:
Next Steps:
Questions & Answers for Recap:
GoInnovateSoftware
Copyright © 2024 GoInnovateSoftware - All Rights Reserved.
Powered by GoDaddy
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.