Loading
0x20Lesson 3 of 6

Make decisions with conditions

Use comparisons and if/else to choose what your program does.

12 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • Write comparison expressions
  • Use if, else if and else
  • Combine conditions with logical operators

A condition is an expression that evaluates to true or false. An if statement runs its block only when the condition is true.

access.js
1const age = 16;
2if (age >= 18) {
3  console.log("Welcome");
4} else {
5  console.log("Ask a grown-up");
6}
Output
Ask a grown-up

Use else if to test another condition. The logical operators && (and), || (or) and ! (not) combine or reverse boolean values.

Key takeaways

  • Comparisons produce booleans.

  • Use braces to group the statements controlled by a condition.

  • &&, || and ! combine boolean expressions.

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

Pass or retry

+25 XP

Read a test score from input. Print Pass for scores of 60 or higher, otherwise print Try again.

  • Passing score
  • Below passing
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: