Code of the Day
BeginnerLanguage basics

Arrays and objects

The two core data structures — ordered lists and keyed records.

JavaScript / TSBeginner9 min read
Recommended first
By the end of this lesson you will be able to:
  • Create and transform arrays with map and filter
  • Create and access objects by key
  • Destructure values out of arrays and objects

Almost all data in JavaScript is held in arrays (ordered lists) and objects (keyed records) — the data-structures fundamentals lesson in JS form. Together they model nearly anything.

Arrays

Ordered, zero-indexed, and full of useful methods:

const nums = [3, 1, 2];
nums[0];          // 3
nums.push(4);     // [3, 1, 2, 4]
nums.length;      // 4

The transforming methods are the heart of idiomatic JS — each takes a function and returns a new array (no mutation):

[1, 2, 3].map((n) => n * 2);        // [2, 4, 6]
[1, 2, 3, 4].filter((n) => n % 2);  // [1, 3]
[1, 2, 3].reduce((a, n) => a + n);  // 6

Objects

An object maps string keys to values:

const user = { name: "Ada", born: 1815 };
user.name;          // "Ada"           (dot access)
user["born"];       // 1815            (bracket access)
user.role = "pioneer";                 // add a key
Object.keys(user);  // ["name", "born", "role"]

Use an object when you look things up by name; an array when order and iteration matter — exactly the "let the operations choose the structure" rule.

Destructuring

A assignment is a clean way to pull values out by position (arrays) or name (objects):

const [first, second] = [10, 20];      // first = 10, second = 20
const { name, born } = user;           // name = "Ada", born = 1815

Destructuring shows up constantly in modern JS, especially in function parameters and imports.

JavaScript — editable, runs in your browser

Where to go next

Next: modules — splitting code across files with import and export.

Finished reading? Mark it complete to track your progress.

On this page