if...else

Runs a block of code only when the condition is true, and otherwise an optional else block.

On this page

Example

#
JavaScript
function grade(points) {
  if (points >= 90) {
    return 'excellent'
  } else if (points >= 70) {
    return 'good'
  } else if (points >= 50) {
    return 'passing'
  } else {
    return 'needs improvement'
  }
}

console.log(grade(95))
console.log(grade(72))
console.log(grade(30))
Console
excellent
good
needs improvement

Definition and usage

#

The if statement checks the condition in the parentheses. If it’s true, it runs the block right after it. The optional else holds code for when the condition is false, and else if lets you check further conditions one after another.

The condition doesn’t have to be true or false itself. JavaScript converts it to a boolean value on its own. The only falsy values are: false, 0, -0, 0n, "", null, undefined, and NaN. Everything else, including an empty array [] and an empty object {}, counts as truthy.

Conditions in an else if chain are checked from the top, and only the first matching block runs. So arrange them from the most specific to the most general.

Syntax

#
Syntax
if (condition) {
  // when true
}

if (condition) {
  // when true
} else {
  // when false
}

if (condition1) {
  // ...
} else if (condition2) {
  // ...
} else {
  // ...
}

More examples

#
Truthy and falsy values in a condition
JavaScript
function welcome(name) {
  if (name) {
    return `Welcome, ${name}!`
  }
  return 'Welcome, stranger!'
}

console.log(welcome('Mia'))
console.log(welcome(''))
console.log(welcome())
Console
Welcome, Mia!
Welcome, stranger!
Welcome, stranger!

An empty string and undefined are both falsy, so they both fall into the second branch. Watch out for the number 0: it’s falsy too, even though it’s often a valid value.

An early return instead of nesting
JavaScript
function buyItem(user, price) {
  if (!user) {
    return 'Log in first'
  }
  if (user.coins < price) {
    return 'Not enough coins'
  }
  user.coins -= price
  return `Purchased, ${user.coins} coins left`
}

console.log(buyItem(null, 50))
console.log(buyItem({ coins: 30 }, 50))
console.log(buyItem({ coins: 80 }, 50))
Console
Log in first
Not enough coins
Purchased, 30 coins left
Combining conditions
JavaScript
const age = 16
const hasConsent = true

if (age >= 18 || hasConsent) {
  console.log('You can join the course')
} else {
  console.log('Parental consent required')
}
Console
You can join the course

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Always use curly braces, even for a single statement. Code without them is easy to break by adding a second line that ends up always running.
  • Instead of deeply nested ifs, check the error cases at the start of a function and return out of it right away.
  • To pick one of two values, the conditional operator condition ? a : b is enough, and with several fixed values a switch often reads better.

Related entries

#

Spotted a mistake or missing an example? Write to us.