๐Ÿ 

๐Ÿ“ฆ JavaScript Arrays and Objects

๐ŸŒŸ Objective: Learn to create, manipulate, and use Arrays and Objects in JavaScript from beginner to advanced level.

๐Ÿ“‘ Table of Contents

๐Ÿ“ฆ Arrays

An array is a list-like object used to store multiple values in a single variable. Arrays are ordered and indexed starting from 0.

โœ… Creating Arrays

const colors = ["red", "green", "blue"];
const numbers = new Array(1, 2, 3);

๐ŸŽฏ Accessing & Updating Elements

console.log(colors[0]); // red
colors[1] = "yellow";   // update second element

๐Ÿ“ Array Length

console.log(colors.length); // 3

๐Ÿ” Looping Through Arrays

for (let i = 0; i < colors.length; i++) {
  console.log(colors[i]);
}
for (let color of colors) {
  console.log(color);
}

๐Ÿงฐ Common Array Methods

colors.push("purple");   // add to end
colors.pop();             // remove from end
colors.shift();           // remove from start
colors.unshift("black"); // add to start

๐Ÿ“ Advanced Methods

const nums = [1, 2, 3, 4];
const doubled = nums.map(n => n * 2); // [2, 4, 6, 8]
const even = nums.filter(n => n % 2 === 0); // [2, 4]
const total = nums.reduce((sum, n) => sum + n, 0); // 10

๐Ÿงฉ Nested Arrays

const matrix = [
  [1, 2],
  [3, 4],
  [5, 6]
];
console.log(matrix[1][0]); // 3

๐Ÿ“˜ Questions and Answers

๐Ÿ” Objects

Objects are used to store data as key-value pairs. They are ideal for representing structured data like a person or product.

โœ… Creating Objects

const person = {
  name: "Ali",
  age: 30,
  city: "Colombo"
};

๐ŸŽฏ Accessing & Updating Properties

console.log(person.name);       // Ali
person.age = 31;
person.country = "Sri Lanka";

๐Ÿงน Deleting Properties

delete person.city;

๐Ÿ” Looping Through Object

for (let key in person) {
  console.log(key + ": " + person[key]);
}

๐Ÿ“ฆ Array Inside Object

const student = {
  name: "Sara",
  grades: [85, 92, 78]
};
console.log(student.grades[1]); // 92

๐Ÿง  Object Inside Array

const products = [
  { name: "Pen", price: 10 },
  { name: "Book", price: 50 }
];
console.log(products[0].name); // Pen

๐Ÿ“˜ Questions and Answers

Summary: Arrays store ordered collections. Objects store named properties. Combining both gives flexibility and power in JavaScript programming.