Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

if — conditional statement

The if statement selects a code branch based on a condition. An optional else handles a false result, while else if checks further conditions in a defined order.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

if (condition) {
  statements
} else if (otherCondition) {
  otherStatements
} else {
  fallbackStatements
}

if is a reserved JavaScript keyword that begins a conditional statement. The condition inside () is evaluated and its result undergoes ToBoolean conversion: a true result executes the first branch, and a false result executes the optional else branch. Without else, a false result skips the statement's body.

The condition need not have the Boolean type: 0 and "" are falsy, whereas "0" and [] are truthy. if is a statement, not an expression, so you cannot assign an if itself to a variable.

else if is a nested if in an else branch, not a separate keyword. Conditions are checked in order; after a branch is selected, the remaining ones are skipped. Without braces, else binds to the nearest matching if.

Use {} blocks to make branch boundaries clear. Do not put ; immediately after if (condition): it creates an empty statement, so the following block is no longer conditional.

Examples

const score = 72;
let result;

if (score >= 90) {
  result = "excellent";
} else if (score >= 60) {
  result = "pass";
} else {
  result = "retry";
}

console.log(result);

Result

pass

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources