Mangcoding

icon chat
Yayan Kurnia Akbar - Thursday, 27 February 2025 - 4 months ago

Conditional statements in JavaScript

single image
Photo By Pankaj Patel on Unsplash

When we write code to create a program, we will often encounter what is called a conditional statement.

Conditional statements are a situation where we provide arguments/options to be executed by the computer if certain criteria are met. To understand this material, it would be beneficial for you to first understand Logic Gates and their usage.

Conditional statements in JavaScript can be created in several ways, depending on the conditions we need.

  • Use if to group one or more commands that will be executed if the condition is met.
  • Use else to group one or more commands that will be executed if the condition is not met.
  • Use else if to group one or more commands that will be executed if the previous condition is not met.
  • Use switch to provide several alternative commands at once if the value of the comparison variable we use is exactly the same.

IF Statement

The general syntax/structure used is :

if (condition) {
    // block of code to be executed if the condition is true
}
Link Copied to Clipboard