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

if...else - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

if...else

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

if (statement) . .

In this article

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

console.log(testNum(-5));
// Expected output: "NOT positive"

js
    if (condition)
       statement1
    [else
       statement2]
condition

.

statement1

. if . ({ ... }) .

statement2

. if .

if...else else if . JavaScript elseif ( ) .

js
    if (1)
       1
    else if (2)
       2
    else if (3)
       3
    ...
    else
       N

, if .

js
    if (1)
       1
    else
       if (2)
          2
       else
          if (3)
    ...

, ({ ... }) . , . if .

js
if () {
  1;
} else {
  2;
}

true () false () truthiness ( ) falsiness ( ) . false, undefined, null, 0, NaN, ("") , false truthy . :

js
var b = new Boolean(false);
if (b) //      (truthy) .

if...else

js
if (cipher_char === from_char) {
  result = result + to_char;
  x++;
} else {
  result = result + clear_char;
}

else if

JavaScript elseif . , else if .

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

. , . , :

js
if (x = y) {
  /* do the right thing */
}

, . :

js
if ((x = y)) {
  /* do the right thing */
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-if-statement


Web Proxy Viewer  |  New URL  |  Original Page