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

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

JavaScript

JavaScript JavaScript (;)

JavaScript

js
{
  statement1;
  statement2;
  // 
  statementN;
}

(if, for, while)

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

{ x++; }

: var

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

2 var x var x C Java 1

let const

JavaScript if...else switch 2

if...else

if true else false

if

js
if (condition) {
  statement1;
} else {
  statement2;
}

condition true false true false Boolean

true statement1 statement2 statement1 statement2 if

else if

js
if (condition1) {
  statement1;
} else if (condition2) {
  statement2;
} else if (conditionN) {
  statementN;
} else {
  statementLast;
}

true ({ /* */ })

if

js
if (condition) {
  //  true 
  // 
} else {
  //  false 
  // 
}

if...else x = y

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

while

false

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

true

: true false Boolean true false

:

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

checkData Text 3 true false

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

switch

switch case

switch

js
switch (expression) {
  case label1:
    statements1;
    break;
  case label2:
    statements2;
    break;
  // 
  default:
    statementsDefault;
}

JavaScript switch

  • case
  • default
    • default
    • default switch
    • default

break

break case switch switch break switch case

fruitType "Bananas" case "Bananas" break switch switch break case "Cherries"

js
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

js
throw expression;

js
throw "Error2"; // 
throw 42; // 
throw true; // 
throw {
  toString() {
    return "";
  },
};

try...catch

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'

js
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

catch try

js
catch (exception) {
  statements
}

catch ( exception) throw

JavaScript catch catch catch

catch

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

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

finally

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

finally finally catch

finally

JavaScript finally finally

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

finally try catch return try...catch...finally

js
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

js
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

try...catch

1 try...catch

try catch

  1. finally
  2. try...catch catch

try...catch try

Error

name message

name Error ( DOMException Error) message

(catch )Error

js
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