LESSON 02 OF 10
Variables and Data Types
Name values and distinguish text, numbers, and booleans.
Before this lesson: complete the preceding lessons or review their examples. No installation or account is needed.
The idea
A variable gives a value a name. Prefer const when you will not reassign that name, and let when it needs a new value. A string is text, a number supports arithmetic, and a boolean is true or false.
At a glance
| Syntax | Purpose |
|---|---|
const | Declare a binding that cannot be reassigned |
let | Declare a binding that can be reassigned |
typeof | Inspect a value’s type |
Try it yourself
Try changing the learner name and number of completed lessons.
Ready. Run the example to open its preview.
Console output
Not run yet.
Page preview
Expected result
Nicole: 3 lessons number
What to watch for
Declaring let score; creates a variable whose value is undefined. null is an explicit empty value. const prevents reassignment, but an object or array stored in a const binding can still be changed.
Your turn
Create a const named topic with the string JavaScript and a let named minutes starting at 10. Add 5 minutes and print both.
Show one solution
const topic = "JavaScript";
let minutes = 10;
minutes += 5;
console.log(topic, minutes);Check your understanding
Can const hold an array that changes?
Yes. You can change the array contents, but you cannot reassign the binding to a different array.