[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/if...else [Back]  [Original]

if...else - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

if...else

Baseline

20157

if...else else

function testNum(a) {
  let result;
  if (a > 0) {
    result = "";
  } else {
    result = "";
  }
  return result;
}

console.log(testNum(-5));
// : ""

js
if (condition)
  statement1

// else 
if (condition)
  statement1
else
  statement2
condition

statement1

condition if ({ /* ... */ })

statement2

condition else if

if...else else if JavaScript 1 elseif

js
if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// 
else
  statementN

js
if (condition1)
  statement1
else
  if (condition2)
    statement2
  else
    if (condition3)
      statement3
// 

({ ... })

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

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");
}

checkValue(1, 3) "a is not 1" dangling else else if

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");
}

if

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");
  }
}

true false Boolean false, undefined, null, 0, -0, NaN, ("") false Boolean

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

if...else

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

else if

JavaScript elseif else if

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

if...else x = y

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

while 1 1

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

while

ECMAScript 2027 LanguageSpecification
# sec-if-statement


Web Proxy Viewer  |  New URL  |  Original Page