View the full blogs:
The control flow is the order in which the computer exectues statements in a script.
Code is typically run from the first line to the last unless told otherwise by structures like conditionals, functions, and loops.
Often times user input is required and if the user messes up and misses something, you can use a conditional to ask them to fix the mistake. For example:
if (field==empty) {
promptUser();
} else {
submitForm();
}
A loop iterates through through all of the fields in the form, checking each one in turn.
A JavaScript function is a block of code designed to perform a particular task and is executed when “something” invokes it.
The code to be executed will go in curly brackets
function name(parameter1, parameter2, parameter3) {
// code to be executed
}
Function invocation occurs when:
Function return - the function will stop executing when a ‘return’ statement is reached. If invoked by a statment, will “return” to execute the code after the invoking statment. Functions often compute a return value - the return value is “returned” back to the “caller”.
Why functions? You can reuse code: define the code once, and use it many times
The () operator invokes the function - accessing a function without () will return the function object instread of the function result.
Functions can be used directly as variable values
Variables declared WITHIN a function become LOCAL to the function and can only be accessed from within that function.
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
** | Exponentiation |
/ | Division |
% | Modulus |
++ | Increment |
– | Decrement |
Operator | Example | Same As |
---|---|---|
= | x = y | x = y |
+- | x += y | x = x + y |
-+ | x -+ y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
**= | x **= y | x = x ** y |
Operator | Description |
---|---|
== | equal to |
=== | equal value and equal type |
!= | not equal |
!== | not equal value or not equal type |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
? | ternary operator |
Operator | Description |
---|---|
&& | logical and |
| | | logical or |
! | logical not |
Operator | Description |
---|---|
typeof | Returns the type of a variable |
instanceof | Returns true if an object is an instance of an object type |