Category: Control Flow in JavaScript

Control Flow in
JavaScript

Explicit Casting

Explicit Casting

Unlike PHP, JavaScript has no explicit casting of types such as (int) or (float). Instead, when you need a value to be of a certain type, use one of JavaScript’s built-in functions, shown in...

The continue Statement

The continue Statement

Sometimes you don’t want to entirely exit from a loop, but instead wish to skip the remaining statements just for this iteration of the loop. In such cases, you can use the continue command....

Breaking Out of a Loop

Breaking Out of a Loop

The break command, which you saw to be important inside a switch statement, is also available within for loops. You might need to use this, for example, when searching for a match of some...

for Loops

for Loops

A for loop combines the best of all worlds into a single looping construct that allows you to pass three parameters for each statement: • An initialization expression • A condition expression • A...

do while Loops

do while Loops

When you require a loop to iterate at least once before any tests are made, use a do…while loop, which is similar to a while loop, except that the test expression is checked only...

while Loops

while Loops

A JavaScript while loop first checks the value of an expression and starts executing the statements within the loop only if that expression is true. If it is false, execution skips over to the...

The switch Statement

The switch Statement

The switch statement is useful when one variable or the result of an expression can have multiple values, for each of which you want to perform a different function. The variable page is mentioned...

The if Statement

The if Statement

Several examples in these tutorials have already made use of if statements. The code within such a statement is executed only if the given expression evaluates to true. Multiline if statements require curly braces...

Using try…catch

Using try…catch

The try and catch keywords are more standard and more flexible than the onError technique shown in the previous section. These keywords let you trap errors for a selected section of code, rather than...

Using onError

Using onError

Here are more constructs not available in PHP. Using either the onError event, or a combination of the try and catch keywords, you can catch JavaScript errors and deal with them yourself.   Events...

The with Statement

The with Statement

The with statement is not one that you’ve seen in earlier topics on PHP, because it’s exclusive to JavaScript. With it (if you see what I mean), you can simplify some types of JavaScript...

Literals and Variables

Literals and Variables

The simplest form of an expression is a literal, which means something that evaluates to itself, such as the number 22 or the string “Press Enter”. An expression could also be a variable, which...

Expressions in JavaScript

Expressions in JavaScript

JavaScript expressions are very similar to those in PHP, an expression is a combination of values, variables, operators, and functions that results in a value; the result can be a number, a string, or...