网页 综合 更新时间 2025-09-19 23:15:13

Control flow and error handling - JavaScript | MDN

自动蜘蛛 developer.mozilla.org 正文约 5199 字
HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
Learn web development
Get to know MDN better
JavaScript supports a compact set of statements, specifically control flow statements, that you can use to incorporate a great deal of interactivity in your application. This chapter provides an overview of these statements.
The JavaScript reference contains exhaustive details about the statements in this chapter. The semicolon (;) character is used to separate statements in JavaScript code.
Any JavaScript expression is also a statement. See Expressions and operators for complete information about expressions.
The most basic statement is a block statement, which is used to group statements. The block is delimited by a pair of curly braces:
Block statements are commonly used with control flow statements (if, for, while).
Here, { x++; } is the block statement.
Note: var-declared variables are not block-scoped, but are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. For example:
This outputs 2 because the var x statement within the block is in the same scope as the var x statement before the block. (In C or Java, the equivalent code would have output 1.)
This scoping effect can be mitigated by using let or const.
A conditional statement is a set of commands that executes if a specified condition is true. JavaScript supports two conditional statements: if...else and switch.
Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false.
An if statement looks like this:
Here, the condition can be any expression that evaluates to true or false. (See Boolean for an explanation of what evaluates to true and false.)
If condition evaluates to true, statement1 is executed. Otherwise, statement2 is executed. statement1 and statement2 can be any statement, including further nested if statements.
You can also compound the statements using else if to have multiple conditions tested in sequence, as follows:
In the case of multiple conditions, only the first logical condition which evaluates to true will be executed. To execute multiple statements, group them within a block statement ({ /* … */ }).
In general, it's good practice to always use block statements—especially when nesting if statements:
In general it's good practice to not have an if...else with an assignment like x = y as a condition:
However, in the rare case you find yourself wanting to do something like that, the while documentation has a Using an assignment as a condition section with guidance on a general best-practice syntax you should know about and follow.
The following values evaluate to false (also known as Falsy values):
All other values—including all objects—evaluate to true when passed to a conditional statement.
Note: Do not confuse the primitive boolean values true and false with the true and false values of the Boolean object!
In the following example, the function checkData returns true if the number of characters in a Text object is three. Otherwise, it displays an alert and returns false.
A switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label. If a match is found, the program executes the associated statement.
A switch statement looks like this:
JavaScript evaluates the above switch statement as follows:
The optional break statement associated with each case clause ensures that the program breaks out of switch once the matched statement is executed, and then continues execution at the statement following switch. If break is omitted, the program continues execution inside the switch statement (and will execute statements under the next case, and so on).
In the following example, if fruitType evaluates to "Bananas", the program matches the value with case "Bananas" and executes the associated statement. When break is encountered, the program exits the switch and continues execution from the statement following switch. If break were omitted, the statement for case "Cherries" would also be executed.
You can throw exceptions using the throw statement and handle them using the try...catch statements.
Just about any object can be thrown in JavaScript. Nevertheless, not all thrown objects are created equal. While it is common to throw numbers or strings as errors, it is frequently more effective to use one of the exception types specifically created for this purpose:
Use the throw statement to throw an exception. A throw statement specifies the value to be thrown:
You may throw any expression, not just expressions of a specific type. The following code throws several exceptions of varying types:
The try...catch statement marks a block of statements to try, and specifies one or more responses should an exception be thrown. If an exception is thrown, the try...catch statement catches it.
The try...catch statement consists of a try block, which contains one or more statements, and a catch block, containing statements that specify what to do if an exception is thrown in the try block.
访问原文 返回搜索 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。