Control flow
Important links
Control Flow
The control flow is the order in which the computer executes statements in a script. Code is run in order from the first line in the file to the last line, unless the computer runs across the (extremely frequent) structures that change the control flow, such as conditionals and loops. Control flow means that when you read a script, you must not only read from start to finish but also look at program structure and how it affects order of execution.
JavaScript Functions
A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when “something” invokes it (calls it).
JavaScript Function Syntax
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().
Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
The parentheses may include parameter names separated by commas: (parameter1, parameter2, …)
The code to be executed, by the function, is placed inside curly brackets: {}
Function Invocation
The code inside the function will execute when “something” invokes (calls) the function:
- When an event occurs (when a user clicks a button)
- When it is invoked (called) from JavaScript code
- Automatically (self invoked)
Functions Used as Variable Values
Functions can be used the same way as you use variables, in all types of formulas, assignments, and calculations.
Local Variables
Variables declared within a JavaScript function, become LOCAL to the function.
Local variables can only be accessed from within the function.
JavaScript Operators
- The assignment operator (=) assigns a value to a variable.
- The addition operator (+) adds numbers:
- The multiplication operator (*) multiplies numbers.
Assignment operators
Assignment operators assign values to JavaScript variables.
- The addition assignment operator (+=) adds a value to a variable.