Summary - Variables
- Node.js course
- Beginner
- Lesson 12/116
- 5 min
Combine let, const, destructuring and spread to write a profile program.
Summary - Variables
In this group you saw the modern way to declare variables: let for mutable, const for fixed, var for old style. Thanks to type inference you do not write the type explicitly; with destructuring and spread/rest you easily extract and merge values.
Now there are two small tasks: one for price calculation, one for extracting values with destructuring. First recall what you learned, then apply it.
Practice Calculate with const
Declare const price = 100 and const tax = 18. Compute total as price + tax. Call console.log(total).
Starter code
// Define price, tax and total
// Log total with console.logSample solution
const price = 100;
const tax = 18;
const total = price + tax;
console.log(total);Expected output
118
Practice Extract values with destructuring
Define an object named point: x is 5, y is 10. Use destructuring to extract x and y. Then call console.log(x + y) to print their sum.
Starter code
const point = { x: 5, y: 10 };
// Extract x and y with destructuring
// Log x + y with console.logSample solution
const point = { x: 5, y: 10 };
const { x, y } = point;
console.log(x + y);Expected output
15
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match the concepts
| Term | Meaning |
|---|---|
let |
Mutable variable |
const |
Fixed variable |
... |
Spread/rest operator |
{ a, b } |
Destructuring |
2 Destructuring reminder
const point = { x: 5, y: 10 };
const ____ = point;
const point = { x: 5, y: 10 };
const { x, y } = point;
Explanation: Curly braces are for object destructuring.