[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Control_flow_and_error_handling [Back]  [Original]

- 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

JavaScript , . .

JavaScript . (;) .

JavaScript . .

In this article

, . .

js
{
  statement_1;
  statement_2;
  
  statement_n;
}

. (if, for, while)

js
while (x < 10) {
  x++;
}

{ x++; } .

: ECMA2015 (6) JavaScript ! JavaScript , . . , .

JavaScript " " C Java . ,

js
var x = 1;
{
  var x = 2;
}
console.log(x); // 2 

2 , var x var x . C Java, 1 .

ECMAScript 2015 let const . let const .

. JavaScript if...else switch .

if...else

if . , else .

if .

js
if (condition) {
  statement_1;
} else {
  statement_2;
}

, condition true false . (Boolean true false .)

condition true statement_1 . statement_2 . statement_1 statement_2 if .

else if .

js
if (condition_1) {
  statement_1;
} else if (condition_2) {
  statement_2;
} else if (condition_n) {
  statement_n;
} else {
  statement_last;
}

, true .

if , if .

js
if (condition) {
  statement_1_runs_if_condition_is_true;
  statement_2_runs_if_condition_is_true;
} else {
  statement_3_runs_if_condition_is_false;
  statement_4_runs_if_condition_is_false;
}

if...else "x = y" .

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

. , while " " .

false . ( )

  • false
  • undefined
  • null
  • 0
  • NaN
  • ("")

true .

: true false Boolean !

,

js
const b = new Boolean(false);
if (b)         //  
if (b == true) //  

, checkData Text 3 true . false .

js
function checkData() {
  if (document.form1.threeChar.value.length == 3) {
    return true;
  } else {
    alert(
      "   . " +
        `${document.form1.threeChar.value}()  .`,
    );
    return false;
  }
}

switch

switch , case case .

switch .

js
switch (expression) {
  case label_1:
    statements_1;
    break;
  case label_2:
    statements_2;
    break;
    
  default:
    statements_default;
}

JavaScript switch .

  • (expression) case , .
  • default .
    • default .
    • default switch .
    • (default , .)

break

case break . break case switch . break switch , case .

fruitType '' '' case . break switch . '' break case '' .

js
switch (fruittype) {
  case "":
    console.log("   $0.59.");
    break;
  case "":
    console.log("   $0.32.");
    break;
  case "":
    console.log("   $0.48.");
    break;
  case "":
    console.log("   $3.00.");
    break;
  case "":
    console.log("   $0.56.");
    break;
  case "":
    console.log("    $2.79.");
    break;
  default:
    console.log(`. ${fruitType} .`);
}
console.log("  ?");

throw , try...catch .

JavaScript throw . , .

throw

throw . throw .

js
throw expression;

. .

js
throw "Error2"; // String
throw 42; // Number
throw true; // Boolean
throw {
  toString: function () {
    return " ";
  },
};

try...catch

try...catch , . , try...catch .

try...catch try , try catch .

, try...catch (try ) catch . try , catch . try catch .

try...catch . getMonthName() months . (1-12) 'InvalidMonthNo' . catch monthName 'unknown' .

js
function getMonthName(mo) {
  mo = mo - 1; //      (1 = Jan, 12 = Dec)
  let months = [
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec",
  ];
  if (months[mo]) {
    return months[mo];
  } else {
    throw "InvalidMonthNo"; //  throw  
  }
}

try {
  //  
  monthName = getMonthName(myMonth); //     
} catch (e) {
  monthName = "unknown";
  logMyErrors(e); //     
}

catch

try catch .

js
catch (catchID) {
  statements;
}

catch throw ( catchID) . , .

JavaScript catch , catch . , catch .

. catch .

js
try {
  throw "myException"; //  
} catch (e) {
  //     
  logMyErrors(e); //     
}

: catch console.log() console.error() . console.error() , .

finally

finally try catch , try...catch...finally .

finally try , catch .

finally . , .

(writeMyFile()) . ( JavaScript ) , finally . finally .

js
openMyFile();
try {
  writeMyFile(theData); //     
} catch (e) {
  handleError(e); //   
} finally {
  closeMyFile(); //   
}

finally , try...catch...finally . try catch .

js
function f() {
  try {
    console.log(0);
    throw "bogus";
  } catch (e) {
    console.log(1);
    return true; // finally     
    console.log(2); //  
  } finally {
    console.log(3);
    return false; //  return 
    console.log(4); //  
  }
  // return false 
  console.log(5); //  
}
console.log(f()); // 0, 1, 3, false

finally catch .

js
function f() {
  try {
    throw "";
  } catch (e) {
    console.log(' "" ');
    throw e; // finally     
  } finally {
    return false; //  throw 
  }
  // return false 
}

try {
  console.log(f());
} catch (e) {
  //   catch !
  // f()  `finally` false 
  //  catch throw   
  console.log('caught outer "bogus"');
}

//  
//  "" 
// false

try...catch

try...catch .

try catch ,

  1. try finally .
  2. try...catch catch .

try...catch try .

Error

Error name message .

name Error (DOMException, Error, ) . message , .

, catch , Error .

js
function doSomethingErrorProne () {
  if (ourCodeMakesAMistake()) {
    throw (new Error(''));
  } else {
    doSomethingToGetAJavascriptError();
  }
}

try {
  doSomethingErrorProne();
}
catch (e) {
  console.log(e.name); // 'Error' 
  console.log(e.message); // ''  JavaScript   
}

Web Proxy Viewer  |  New URL  |  Original Page