| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Control_flow_and_error_handling | [Back] [Original] |
Get to know MDN better
JavaScript
JavaScript JavaScript (;)
{
statement1;
statement2;
//
statementN;
}
(if, for, while)
while (x < 10) {
x++;
}
{ x++; }
JavaScript if...else switch 2
if true else false
if
if (condition) {
statement1;
} else {
statement2;
}
condition true false true false Boolean
true statement1 statement2 statement1 statement2 if
else if
if (condition1) {
statement1;
} else if (condition2) {
statement2;
} else if (conditionN) {
statementN;
} else {
statementLast;
}
true ({ /* */ })
if
if (condition) {
// true
//
} else {
// false
//
}
if...else x = y
if (x = y) {
/* */
}
falseundefinednull0NaN"") true
:
true false Boolean true false
:
const b = new Boolean(false);
if (b) {
// true
}
if (b == true) {
// false
}
checkData Text 3 true false
function checkData() {
if (document.form1.threeChar.value.length === 3) {
return true;
}
alert(
` 3 ${document.form1.threeChar.value} `,
);
return false;
}
switch case
switch
switch (expression) {
case label1:
statements1;
break;
case label2:
statements2;
break;
//
default:
statementsDefault;
}
JavaScript switch
case default
default default switch default break case switch switch break switch case
fruitType "Bananas" case "Bananas" break switch switch break case "Cherries"
switch (fruitType) {
case "Oranges":
console.log("Oranges are $0.59 a pound.");
break;
case "Apples":
console.log("Apples are $0.32 a pound.");
break;
case "Bananas":
console.log("Bananas are $0.48 a pound.");
break;
case "Cherries":
console.log("Cherries are $3.00 a pound.");
break;
case "Mangoes":
console.log("Mangoes are $0.56 a pound.");
break;
case "Papayas":
console.log("Papayas are $2.79 a pound.");
break;
default:
console.log(`Sorry, we are out of ${fruitType}.`);
}
console.log("Is there anything else you'd like?");
throw try...catch
JavaScript
throw
throw expression;
throw "Error2"; //
throw 42; //
throw true; //
throw {
toString() {
return "";
},
};
try...catch 1 try...catch
try...catch 1 try try catch
try catch try try catch try catch finally try catch try...catch
try...catch (112) 'Invalid month code' catch monthName 'unknown'
function getMonthName(mo) {
mo--; // (0 = Jan, 11 = Dec)
// prettier-ignore
const months = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
if (!months[mo]) {
throw new Error("Invalid month code"); // throw
}
return months[mo];
}
try {
//
monthName = getMonthName(myMonth); //
} catch (e) {
monthName = "unknown";
logMyErrors(e); //
}
catch try
catch (exception) {
statements
}
catch ( exception) throw
JavaScript catch catch catch
catch
try {
throw "myException"; //
} catch (err) {
//
logMyErrors(err); //
}
:
catch console.log() console.error()
finally try catch finally try...catch...finally
finally finally catch
finally
JavaScript finally finally
openMyFile();
try {
writeMyFile(theData); //
} catch (e) {
handleError(e); //
} finally {
closeMyFile(); //
}
finally try catch return try...catch...finally
function f() {
try {
console.log(0);
throw "bogus";
} catch (e) {
console.log(1);
// finally
//
return true;
console.log(2); //
} finally {
console.log(3);
return false; // "return"
// `f`
console.log(4); //
}
console.log(5); //
}
console.log(f()); // 0, 1, 3, false
finally catch
function f() {
try {
throw "bogus";
} catch (e) {
console.log('caught inner "bogus"');
// throw finally
//
throw e;
} finally {
return false; // "throw"
// `f`
}
}
try {
console.log(f());
} catch (e) {
//
// f() `finally` false
// `catch` `throw`
console.log('caught outer "bogus"');
}
// Logs:
// caught inner "bogus"
// false
1 try...catch
try catch
finally try...catch catch name message
name Error ( DOMException Error) message
(catch )Error
function doSomethingErrorProne() {
if (ourCodeMakesAMistake()) {
throw new Error("");
}
doSomethingToGetAJavaScriptError();
}
try {
doSomethingErrorProne();
} catch (e) {
// `console.error()`
console.error(e.name); // 'Error'
console.error(e.message); // '' JavaScript
}
| Web Proxy Viewer | New URL | Original Page |