| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Control_flow_and_error_handling | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
JavaScript , . .
JavaScript . (;) .
, . .
{
statement_1;
statement_2;
statement_n;
}
. (if, for, while)
while (x < 10) {
x++;
}
{ x++; } .
: ECMA2015 (6) JavaScript ! JavaScript , . . , .
JavaScript " " C Java . ,
var x = 1;
{
var x = 2;
}
console.log(x); // 2
2 , var x var x . C Java, 1 .
. JavaScript if...else switch .
if...else if . , else .
if .
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 .
if (condition_1) {
statement_1;
} else if (condition_2) {
statement_2;
} else if (condition_n) {
statement_n;
} else {
statement_last;
}
, true .
if , if .
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" .
if ((x = y)) {
/* */
}
falseundefinednull0NaN"") true .
, checkData Text 3 true . false .
function checkData() {
if (document.form1.threeChar.value.length == 3) {
return true;
} else {
alert(
" . " +
`${document.form1.threeChar.value}() .`,
);
return false;
}
}
switch switch , case case .
switch .
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 , .) case break . break case switch . break switch , case .
fruitType '' '' case . break switch . '' break case '' .
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 .
throw expression;
. .
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' .
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 .
catch (catchID) {
statements;
}
catch throw ( catchID) . , .
JavaScript catch , catch . , catch .
. catch .
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 .
openMyFile();
try {
writeMyFile(theData); //
} catch (e) {
handleError(e); //
} finally {
closeMyFile(); //
}
finally , try...catch...finally . try catch .
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 .
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 finally .try...catch catch .Error name message .
name Error (DOMException, Error, ) . message , .
, catch , Error .
function doSomethingErrorProne () {
if (ourCodeMakesAMistake()) {
throw (new Error(''));
} else {
doSomethingToGetAJavascriptError();
}
}
try {
doSomethingErrorProne();
}
catch (e) {
console.log(e.name); // 'Error'
console.log(e.message); // '' JavaScript
}
This page was last modified on 2025 10 9 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |