Conditionals in JavaScript

Introduction to the syntax and semantics of conditionals in JavaScript.

Conditionals features

A program conditional has the following:

  • A condition which can be true or false
  • A program block executed when the condition is true
  • A program block executed when the condition is false

With these features in mind, let’s map them to the corresponding JavaScript syntax.

Keywords and semantics

In JavaScript, a conditional statement consists of various properties and keywords.

if condition

The if-condition is the starting point of a conditional. The syntax is as follows.

// Execute these instructions

The <condition> here could be any Boolean value that is resolved within the if(..) brackets or even before it.

For the example in the previous lesson, we can use results of comparison operators to help identify whether var is 1, -1, or 0.

if(variable === 0){
// Execute these instructions

The code below shows the if-condition in action.

Get hands-on with 1200+ tech skills courses.