Latticework

Command Palette

Search for a command to run...

JavaScript

Syntax & Variables

14 min

Explanation

Modern JavaScript variables are declared with const (can't be reassigned) or let (can) — var still exists but is legacy, avoid it in new code. Destructuring pulls values out of objects (const { name, age } = user) or arrays (const [first, second] = list) in one step instead of repeated .property/[index] access. Template literals (backtick strings) embed expressions directly with ${...}, instead of string concatenation with +.

const user = { name: "Ada", age: 30 };
const { name, age } = user;
console.log(`${name} is ${age} years old`);
// Ada is 30 years old
Try it

Destructuring works the same way for objects (by key) and arrays (by position) — both just unpack a structure into named variables in one line instead of several.

Loading editor…
Exercise

Write `formatUser(user)` — `user` is an object `{ name, age }`. Destructure it and return the string `"<name> is <age> years old"` using a template literal.

Quiz

What's the difference between `const` and `let`?

Checkpoint

You can destructure objects and arrays, and build strings with template literals instead of concatenation.