Skip to content
Science, Maths & Technology

Programming for Selection

Updated Wednesday, 24th June 2015

Start learning how to program conditional JavaScript using if statements.

This page was published over five years ago. Please be aware that due to the passage of time, the information provided on this page may be out of date or otherwise inaccurate, and any views or opinions expressed may no longer be relevant. Some technical elements such as audio-visual and interactive media may no longer work. For more detail, see our Archive and Deletion Policy

if Statements

Computer program Copyrighted  image Icon Copyright: Dreamstime  This section aims to:

  • explain the use of comparison operators to decide whether a condition is true or false;
  • introduce simple conditional statements;
  • explain how several simple statements can be wrapped together to form a compound statement;
  • introduce more complex conditional statements;
  • describe how to express the above conditions and statements in program code.

The programs in the last section (Getting Started) were rather simple: the computer carried out the JavaScript instructions sequentially. In this section, we consider how to instruct the computer to act differently according to certain conditions;
i.e. either to execute a sequence of one or more instructions in a program or to ‘jump over’ them (miss them out). For example, we may wish to input a number representing an exam mark, and display the word Pass if it is 40 or above, or Fail if it is below 40. In this case the condition that needs to be checked is whether the number is greater than or equal to 40. If it is, the instruction to display Pass is executed and the instruction to display Fail is skipped over. If it is not, the instruction to display Pass is skipped and the instruction to display Fail is executed.

The method for doing this is known as an 'if' statement, and you'll be learning more about what you need to do in order to use these in the next article, 'Booleans and the Comparison Operators'.




Related content (tags)

Copyright information

For further information, take a look at our frequently asked questions which may give you the support you need.

Have a question?