Class 07 Reading Notes

Class 07 Reading Notes

View the full blogs:

MDN Control Flow

JavaScript Functions

JavaScript Operators

Control Flow

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.

JavaScript Functions

A JavaScript function is a block of code designed to perform a particular task and is executed when “something” invokes it.

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.

JavaScript Arithmetic Operators

Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus
++ Increment
Decrement

JavaScript Assignment Operators

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

JavaScript Comparision Operators

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

JavaScript Logical Operators

Operator Description
&& logical and
| | logical or
! logical not

JavaScript Type Operators

Operator Description
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type

Return home