3. Modern JavaScript Fundamentals
JavaScript Arrays, Objects & Modern ES6 Methods
Master real-world JavaScript data structures: objects, arrays, destructuring, spread syntax (...), for...of loops, and .map(), .filter(), .find(), and .reduce().
- Model structured records using JavaScript objects and ordered lists with arrays
- Unpack properties cleanly with destructuring and copy data safely using the spread operator (...)
- Transform, search, filter, and summarize lists using .map(), .filter(), .find(), and .reduce()
Almost every real web app works with lists of structured items: a feed of social posts, products in a shopping cart, rows in a study planner, or courses in a catalog. In JavaScript, you represent each item as an object { ... } and a collection of items as an array [ ... ].
Objects & Arrays: Storing Structured Data
An object groups related named properties (key: value), while an array holds an ordered zero-indexed list:
const course = {
title: "Web Foundations",
lessons: 16,
free: true,
};
// Dot notation (most common) vs bracket notation (dynamic keys)
console.log(course.title); // "Web Foundations"
console.log(course["free"]); // true
const tracks = ["HTML", "CSS", "JavaScript"];
console.log(tracks[0]); // "HTML"
console.log(tracks.length); // 3Notice also optional chaining (?.): writing user.profile?.city safely returns undefined instead of crashing the whole app if profile happens to be missing on a newly created account!
ES6 Superpowers: Destructuring & Spread (...)
Before exploring array methods, learn two shorthand syntaxes you will see constantly in modern JavaScript, React, and Next.js:
- Destructuring unpacks properties into standalone variables in one line:
const student = { fullName: "Maya Chen", track: "Web Dev", streak: 12 };
const { fullName, streak } = student;
console.log(fullName, streak); // "Maya Chen" 12- Spread (
...) copies items or properties into a brand-new array or object without mutating the original:
const baseTasks = ["Read Lesson 1", "Try Playground"];
const updatedTasks = [...baseTasks, "Pass Quiz"]; // New array! baseTasks stays untouched
const updatedStudent = { ...student, streak: 13 }; // Copies fields, overrides streakLooping vs Transforming (.filter(), .map(), .find(), .reduce())
To run code once per element without needing index variables (i = 0), reach for for...of:
for (const track of ["HTML", "CSS", "JS"]) {
console.log("Learning:", track);
}When you want to compute a new result from an array without mutating the original array, modern JavaScript provides four declarative array methods:
| Method | Question It Answers | Returns |
|---|---|---|
.filter(fn) | "Which items match this rule?" | A new array of matching items (0 to many) |
.find(fn) | "What is the FIRST item matching this rule?" | One item (or undefined if none found) |
.map(fn) | "How should each item be transformed?" | A new array of exact same length with transformed values |
.reduce(fn, start) | "How do all items combine into one summary value?" | A single value (sum total, max number, count object) |
Because .filter() and .map() both return clean arrays, you can chain them top-to-bottom like an assembly line:
const courses = [
{ title: "Python", hours: 20, featured: true },
{ title: "Git", hours: 8, featured: false },
{ title: "Web Dev", hours: 15, featured: true },
];
const featuredTitles = courses
.filter((course) => course.featured)
.map((course) => `${course.title} (${course.hours}h)`);
console.log(featuredTitles); // ["Python (20h)", "Web Dev (15h)"]Try It Live: Filter, Map & Total Revenue
Practice inspecting, filtering, and totaling real product objects below—try adding .find() to locate "Tea" by name:
Click “Run Code” to execute this snippet right in your browser.
Methods like .push() and .sort() modify the original array in place. Whenever you want the original array untouched, copy it first with spread syntax: const sorted = [...scores].sort((a, b) => b - a);.
Quick Checkpoints
What does [1, 2, 3, 4].filter((n) => n > 2) return?
Which method stops as soon as it locates the SINGLE matching item (for example, finding a user by id) instead of scanning for an array of matches?