ES6+ Review
Objectives
By the end of this lesson, you should be able to:
- Use
let/const, arrow functions, and template literals confidently - Destructure objects and arrays to pull out values directly
- Use the spread and rest operators correctly
💡 Why this matters: This is a working review, not a first introduction, real Node.js and Express code (starting in Module 5) uses every one of these features constantly. Being fluent here means the rest of this course reads naturally instead of stopping to decode syntax.
⚠️ A note on verification: every snippet and output in this lesson was actually run with Node.js.
let, const, and Template Literals
const name = "Priya";
let age = 29;
age += 1;
const greet = (person) => `Hello, ${person}!`;
console.log(greet(name));
Hello, Priya!
const declares a binding that can’t be reassigned, let declares one that can, both are block-scoped (unlike the older var). Template literals (backticks, with ${...} for interpolation) replace manual string concatenation, greet(name) reads directly instead of "Hello, " + person + "!".
Destructuring
const user = { name: "Priya", role: "Engineer" };
const { name: userName, role } = user;
console.log(userName, role);
Priya Engineer
Destructuring pulls values directly out of an object (or array) into their own variables in one step. { name: userName, role } renames name to userName while it’s extracted (since name was already used above), and keeps role as-is.
Spread and Rest
const numbers = [1, 2, 3];
const moreNumbers = [...numbers, 4, 5];
console.log(moreNumbers);
[ 1, 2, 3, 4, 5 ]
...numbers here is the spread operator, expanding an existing array’s elements into a new one. The same ... syntax means something different in a function’s parameter list:
function sum(...values) {
return values.reduce((total, n) => total + n, 0);
}
console.log(sum(1, 2, 3, 4));
10
Here, ...values is the rest parameter, collecting any number of arguments into a single array inside the function. Spread expands, rest collects, same syntax, opposite direction, and which one applies depends on where it’s used.
Try It
- Write an arrow function
formatPricethat takes a number and returns a string like"$24.99", using a template literal. - Destructure
{ id: 1, title: "Node Basics", published: true }into three separate variables in one line. - Write a function
combine(...items)using a rest parameter that returns all its arguments joined into a single comma-separated string. - Use the spread operator to merge two arrays,
[1, 2]and[3, 4], into one array of four elements.
Recap
constandletare block-scoped, template literals interpolate values directly into a string with${...}.- Destructuring pulls values out of an object or array in one step, optionally renaming them.
- Spread (
...) expands an array or object, rest (...) collects multiple arguments into an array, same syntax, opposite direction.
Next lesson: modules, importing and exporting code across files.