O'Reilly logo

JavaScript® 24-Hour Trainer by Jeremy McPeak

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 4. Making Decisions

Programming is much more than variables, functions, and calculations. If programming were as simple as those three things, computers wouldn't be anything more than giant calculators, and chances are good you wouldn't be reading this book.

What sets computers apart from other electronic calculation devices is their capability to simulate intelligence. Granted, it's the programmer who tells the computer how to behave and how and when to make decisions, but it's the computer that decides what action to take based on a set of criteria.

Making decisions is an important part of programming. At the core of decision making are conditions, and the program makes a decision based on whether or not a particular condition is met. There are a few control structures that use conditions to determine how code executes, but first let's look at how to write conditional statements.

CONDITIONAL STATEMENTS

In JavaScript, a conditional statement consists of an operation comparing two or more values to result in a Boolean value together with statements that may run, depending on the result. Recall from Lesson 2 that Boolean values consist of either true or false. A condition is said to have been met if the conditional statement results in true; otherwise, a value of false defines the condition as unmet. In order to get a true or false value, you typically use JavaScript's comparison operators.

Comparison Operators

Lesson 2 introduced several operators: the assignment operator to give ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required