Make decisions with conditions
Use comparisons and if/else to choose what your program does.
- 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.
1const age = 16;
2if (age >= 18) {
3 console.log("Welcome");
4} else {
5 console.log("Ask a grown-up");
6}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.
Pass or retry
Read a test score from input. Print Pass for scores of 60 or higher, otherwise print Try again.
- Passing score
- Below passing
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…