Loading
0x10Lesson 2 of 6

Values and variables

Name data with const and let, and work with JavaScript’s basic value types.

12 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • Create variables with const and let
  • Distinguish strings, numbers and booleans
  • Convert text input to a number

A variable gives a name to a value. Use const when you will not assign a different value later, and let when you will. Prefer const by default.

values.js
1const language = "JavaScript";
2let year = 1995;
3const isFun = true;
4
5console.log(language, year, isFun);
Output
JavaScript 1995 true

JavaScript has strings, numbers and booleans (true or false). The exercise input arrives as text, so convert it with Number(input) before doing arithmetic.

Key takeaways

  • Use const by default and let for reassigned values.

  • JavaScript distinguishes strings, numbers and booleans.

  • Convert text to a number with Number(value).

Lesson quiz

5 questions · pass with 4 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write JavaScript

Write the program, run it against the test cases, and use the feedback to build your JavaScript skills.

Exercise 1

Price with tax

+25 XP

Read a price from input, add 8% tax, and print the total with two decimal places.

  • $10 purchase
  • $25 purchase
main.js
Loading editor…

JavaScript runs in a sandboxed browser frame with a 1.5 second time limit. Input is available as the string variable input.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: