logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

Scroll

Javascript lesson 2 The if construct

16/04/2025, by Ivan

In this lesson, we will explore how the if statement can be used in JavaScript. The syntax in JavaScript is quite similar to PHP (both were designed with C++ programmers in mind), so you can also read about if statements in this PHP lesson:

PHP Lessons – Lesson 8 – If Statement

As an example, let's take a look at the following if structure:

var x = 15;
var y = 7;

if (x > y) {
  alert('x is greater than y'); // if x is greater than y
} else {
  alert('x is less than y'); // if x is not greater than y
}

The general form of an if statement is as follows:

if (condition) {
  action if the condition is true
} else {
  action if the condition is false
}