|
| 1 | +// 📌 JavaScript Intermediate - Arrays and Objects |
| 2 | + |
| 3 | +// Welcome to the first section of the JavaScript Intermediate tutorial! |
| 4 | +// Here, you'll learn how to work with arrays and objects more efficiently. |
| 5 | + |
| 6 | +// Arrays - Ordered collections of values |
| 7 | +let fruits = ["Apple", "Banana", "Cherry"]; |
| 8 | +console.log("Fruits:", fruits); |
| 9 | + |
| 10 | +// Accessing elements |
| 11 | +console.log("First fruit:", fruits[0]); |
| 12 | + |
| 13 | +// Adding and removing elements |
| 14 | +fruits.push("Orange"); // Adds at the end |
| 15 | +console.log("After push:", fruits); |
| 16 | +fruits.pop(); // Removes last element |
| 17 | +console.log("After pop:", fruits); |
| 18 | + |
| 19 | +// Iterating over arrays |
| 20 | +fruits.forEach((fruit, index) => { |
| 21 | + console.log(`Fruit ${index + 1}: ${fruit}`); |
| 22 | +}); |
| 23 | + |
| 24 | +// Objects - Collections of key-value pairs |
| 25 | +let person = { |
| 26 | + name: "Alice", |
| 27 | + age: 25, |
| 28 | + city: "New York", |
| 29 | +}; |
| 30 | +console.log("Person:", person); |
| 31 | + |
| 32 | +// Accessing object properties |
| 33 | +console.log("Name:", person.name); |
| 34 | +console.log("Age:", person["age"]); |
| 35 | + |
| 36 | +// Adding and modifying properties |
| 37 | +person.job = "Engineer"; |
| 38 | +console.log("Updated person:", person); |
| 39 | + |
| 40 | +// Object iteration |
| 41 | +for (let key in person) { |
| 42 | + console.log(`${key}: ${person[key]}`); |
| 43 | +} |
| 44 | + |
| 45 | +// 💡 Practice working with arrays and objects using different methods! |
0 commit comments