Today's notes cover JavaScript control flow and error handling.
I believe the most important take-aways are 1) Javascript's uses “falsey” values and that in try…catch…finally statements, the finally
statement executives in all cases, whether or not an exception is throw.
The semicolon (;
) character is used to separate statements in JavaScript code.
Any JavaScript expression is also a statement.
Block Statements
The most basic statement is a block statement that is used to group statements.
JavaScript prior to ECMAScript2015 (6th edition) does not have block scope.
Variables introduced within a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself.
Block statements in JavaScript do not define a scope.
Conditional Statements
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
.
if…else statements
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.
“Falsy” values in JavaScript
The following values evaluate to false:
false
undefined
null
0
NaN
- the empty string (
""
)
All other values, including all objects, evaluate to true when passed to a conditional statement.
switch statement
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.
Exception Handling Statements
You can throw exceptions using the throw
statement and handle them using the try...catch
statements.
throw
statement
Use the throw
statement to throw an exception. When you throw an exception, you specify the expression containing the value to be thrown:
You can throw any expression, not just a specific type.
In other words, you can throw strings, functions, objects, numbers, etc.
throw 'Error2'; // String type
throw 42; // Number type
throw true; // Boolean type
throw {toString: function() { return "I'm an object!"; } };
When an exception is thrown, it will be tried by a try...catch
statement or a try...catch...finally
statement.
try...catch
statement
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.
That is, you want the try
block to succeed, and if it does not succeed, you want control to pass to the catch
block.
If any statement within the try
block (or in a function called from within the try
block) throws an exception, control immediately shifts to the catch
block.
If no exception is thrown in the try
block, the catch
block is skipped.
try...catch...finally
statement
The finally
block executes after the try
and catch
blocks execute but before the statements following the try...catch
statement.
It is also important to note that the finally
block will execute whether or not an exception is thrown.
If an exception is thrown, however, the statements in the finally
block executes even if no catch
block handles the exception that was thrown.
You can use the finally
block to make your script fail gracefully when an exception occurs; for example, you may need to release a resource that your script has tied up.
Leave a Reply