start learning
Image 1

If statement

An "if statement" in programming, including JavaScript, is a fundamental control structure that allows you to conditionally execute a block of code based on whether a specified condition evaluates to true or false. It's a way to introduce decision-making into your programs, allowing different parts of the code to be executed depending on the outcome of the condition.

The basic syntax of an if statement in JavaScript is as follows:


if (condition) {
    // Code to execute if the condition is true
}

Here's how the if statement works:

  1. The condition is an expression that evaluates to either true or false.
  2. If the condition is true, the block of code within the curly braces {} is executed.
  3. If the condition is false, the block of code within the curly braces is skipped, and the program continues to the next statement after the if statement.
Example:

var temperature = 25;

if (temperature > 30) {
    console.log("It's hot outside!");
}

console.log("Program continues...");

In this example, if the temperature is greater than 30, the message "It's hot outside!" will be printed. Otherwise, only "Program continues..." will be printed.