Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

if...else

BaselineWidely available

Theif...else statement executes a statement if a specified condition istruthy. If the condition isfalsy, another statement in the optionalelse clause will be executed.

Try it

function testNum(a) {  let result;  if (a > 0) {    result = "positive";  } else {    result = "NOT positive";  }  return result;}console.log(testNum(-5));// Expected output: "NOT positive"

Syntax

js
if (condition)  statement1// With an else clauseif (condition)  statement1else  statement2
condition

An expression that is considered to be eithertruthy orfalsy.

statement1

Statement that is executed ifcondition istruthy. Can be any statement, including further nestedif statements. To execute multiple statements, use ablock statement ({ /* ... */ }) to group those statements. To execute no statements, use anempty statement.

statement2

Statement that is executed ifcondition isfalsy and theelse clause exists. Can be any statement, including block statements and further nestedif statements.

Description

Multipleif...else statements can be nested to create anelse if clause. Note that there is noelseif (in one word) keyword in JavaScript.

js
if (condition1)  statement1else if (condition2)  statement2else if (condition3)  statement3// …else  statementN

To see how this works, this is how it would look if the nesting were properly indented:

js
if (condition1)  statement1else  if (condition2)    statement2  else    if (condition3)      statement3// …

To execute multiple statements within a clause, use a block statement ({ /* ... */ }) to group those statements.

js
if (condition) {  statements1} else {  statements2}

Not using blocks may lead to confusing behavior, especially if the code is hand-formatted. For example:

js
function checkValue(a, b) {  if (a === 1)    if (b === 2)      console.log("a is 1 and b is 2");  else    console.log("a is not 1");}

This code looks innocent — however, executingcheckValue(1, 3) will log "a is not 1". This is because in the case ofdangling else, theelse clause will be connected to the closestif clause. Therefore, the code above, with proper indentation, would look like:

js
function checkValue(a, b) {  if (a === 1)    if (b === 2)      console.log("a is 1 and b is 2");    else      console.log("a is not 1");}

In general, it is a good practice to always use block statements, especially in code involving nestedif statements.

js
function checkValue(a, b) {  if (a === 1) {    if (b === 2) {      console.log("a is 1 and b is 2");    }  } else {    console.log("a is not 1");  }}

Do not confuse the primitive Boolean valuestrue andfalse with truthiness or falsiness of theBoolean object. Any value that is notfalse,undefined,null,0,-0,NaN, or the empty string (""), and any object, including a Boolean object whose value isfalse, is consideredtruthy when used as the condition. For example:

js
const b = new Boolean(false);if (b) {  console.log("b is truthy"); // "b is truthy"}

Examples

Using if...else

js
if (cipherChar === fromChar) {  result += toChar;  x++;} else {  result += clearChar;}

Using else if

Note that there is noelseif syntax in JavaScript. However, you can write it with a space betweenelse andif:

js
if (x > 50) {  /* do something */} else if (x > 5) {  /* do something */} else {  /* do something */}

Using an assignment as a condition

You should almost never have anif...else with an assignment likex = y as a condition:

js
if ((x = y)) {  // …}

Because unlikewhile loops, the condition is only evaluated once, so the assignment is only performed once. The code above is equivalent to:

js
x = y;if (x) {  // …}

Which is much clearer. However, in the rare case you find yourself wanting to do something like that, thewhile documentation has aUsing an assignment as a condition section with our recommendations.

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-if-statement

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp