*
Previous Javascript Word Counter Javascript Array Methods Next

Arrays & Objects in JavaScript

πŸ“˜ Arrays & Objects in JavaScript

Arrays and Objects are the two most important data structures in JavaScript. Let’s break them down clearly with examples and a mini project.

πŸ”Ή Arrays

An array is an ordered collection of values stored in a single variable. Arrays can hold numbers, strings, objects, or even other arrays.

βœ… Example: Creating an Array

let fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // apple
console.log(fruits[2]); // cherry
    
JavaScript Arrays β€” diagram Arrays in JavaScript β€” structure & common operations Example array: let fruits = ["apple", "banana", "cherry"]; index 0 "apple" index 1 "banana" index 2 "cherry" [0] [1] [2] push("date") β†’ adds at index 3 index 3 "date" β†’ after push pop() β†’ removes last element ("date")
  • Arrays are ordered collections, indexed from 0.
  • Array lielements can be any type: primitives, objects, other arrays, functions.
  • Arrays are reference types β€” assigning copies the reference, not the values.

Property: length, fruits.length // 3

length returns the number of elements in the array (always integer β‰₯ 0) Note: setting length can truncate or expand (with empty slots)

Iterating / common methods:
      fruits.forEach(f => console.log(f));
      let upper = fruits.map(f => f.toUpperCase());
      let bananas = fruits.filter(f => f === 'banana');

map/filter/reduce are functional-style helpers that return new arrays/values.

πŸ”§ Common Array Methods

let numbers = [10, 20, 30];

// Add elements
numbers.push(40); // [10,20,30,40]

// Remove last element
numbers.pop();    // [10,20,30]

// Remove first element
numbers.shift();  // [20,30]

// Add element at beginning
numbers.unshift(5); // [5,20,30]

// Loop through array
numbers.forEach(num => console.log(num));
    

πŸ”Ή Objects

An object is a collection of key–value pairs. Keys are strings (or Symbols), and values can be any data type.

βœ… Example: Creating an Object

let user = {
  name: "Alice",
  age: 25,
  isStudent: true
};

console.log(user.name);  // Alice
console.log(user["age"]); // 25
    

πŸ”§ Adding / Updating / Deleting Properties

user.country = "USA";    // add
user.age = 26;           // update
delete user.isStudent;   // delete
console.log(user);
    

🌍 Combining Arrays & Objects

Often, we combine arrays and objects to represent structured data.

// Array of objects
let users = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 }
];

console.log(users[1].name); // Bob

// Object with array property
let classroom = {
  teacher: "Mr. Smith",
  students: ["Alice", "Bob", "Charlie"]
};

console.log(classroom.students[2]); // Charlie
    

4️⃣ Mini Project: Simple Contact List

const contacts = [
  { name: "Rahul", phone: "9876543210" },
  { name: "Priya", phone: "9123456780" },
  { name: "Amit", phone: "9988776655" }
];

// Add a new contact
contacts.push({ name: "Sneha", phone: "9001122334" });

// Find a contact by name
function findContact(name) {
  return contacts.find(contact => contact.name === name);
}

console.log(findContact("Priya")); 
// { name: "Priya", phone: "9123456780" }
  

πŸ”‘ Key Points

  • Use arrays for ordered collections of items.
  • Use objects for structured data with named properties.
  • You can nest arrays inside objects and vice versa.
  • Both arrays and objects are reference types in JavaScript.
Back to Index
Previous Javascript Word Counter Javascript Array Methods Next
*
*