Chapter 12. Conditions: choosing code to run

This chapter covers

  • Comparing values with comparison operators
  • Checking conditions that are true or false
  • The if statement—running code only if a condition is met
  • The else clause—running code when a condition is not met
  • Making sure user input won’t break your code
  • Generating random numbers with Math.random()

So far, all of your code follows a single path. When you call a function, every statement in the function body is executed. You’ve managed to get a lot done and covered quite a few core ideas in JavaScript but your programs have lacked flexibility; they haven’t been able to decide whether or not to execute blocks of code.

In this chapter you learn how to run code only when ...

Get Get Programming with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.