CodeBlanks Logo Icon CodeBlanks

Summary - Variables

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

index.js
// Define price, tax and total
// Log total with console.log

Sample solution

index.js
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

index.js
const point = { x: 5, y: 10 };
// Extract x and y with destructuring
// Log x + y with console.log

Sample solution

index.js
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

Question
const point = { x: 5, y: 10 };
const ____ = point;
Answer
const point = { x: 5, y: 10 };
const { x, y } = point;

Explanation: Curly braces are for object destructuring.

All lessons in Beginner