JavaScript | Programming | Decisions | Objects
Decisions are logical choices made based on the evaluation of variables and functions. Functions are self-contained blocks of code that can be reused. A function makes a decision depending on the evaluation of a condition. A condition is declared within ( ) parenthesis. A function evaluates a condition then generates an action.
The if() function evaluates whether a condition is true or false. If the condition evaluates true, the statement is executed. If the condition evaluates false, the statement is not executed...
The condition is true. Using the document.write() function, the output is...
6 is less than 12.
This is an example using the && and operator. Multiple statements are declared within { } curly brackets...
Both conditions are true so the statements are executed. The alert statement executes first. The output is...
6 is less than 12...
and 6 is greater than 3.
The else command generates alternative statements, one if the condition is true, or another if the condition is false. The { } curly brackets are not required for single statements, but are used for readability...
The condition is false. The output is...
18 is greater than 12.
The else if() function adds another condition to the if() function...
The output is...
18 is less than 24.
There is an alternative to evaluating a condition with ternary operators. The ? question mark is an operator that processes like the if() function and the : colon is an operator that processes like the else command...
There is a third alternative to evaluating a condition. The switch() function evaluates a variable as a condition. The case command compares the variable with a value. The : operator declares that the following statement is dependent on the condition. The break command stops the evaluation if the condition is true. The default command evaluates if all statements are false...
Loops are functions that repeat. The while() function is like the if() function, but the statement repeats when the condition evaluates true. When the condition evaluates false, the function stops looping. At the end of the statement block is a statement that changes the output of the condition...
num++ changes the output of the condition. The output is...
1 2 3 4 5 6 7 8 9 10
The do command generates a statement before the while() function evaluates the condition. If the condition evaluates true, the statement repeats. If the condition evaluates false, the function stops...
The output is...
1 2 3 4 5 6 7 8 9 10
The for() function is an alternative to the while() function...
The output is...
1 2 3 4 5 6 7 8 9 10
User functions are created by the user and are made up of variables and functions. The function command declares a new user function, followed by the name of the function. User function names are created with the same naming conventions as a variable. Variables inside a user function have a local scope. Variables outside a user function have a global scope. A variable inside a function is global if declared without the var keyword. The statement of a user function is declared within { } side brackets. A user function is called by its name when needed...
The output is...
Hello universe!
This is another example...
The output is...
Hello universe!
An argument can be passed when calling the function. An argument is a value passed to the function and stored in a variable local to the function. The argument variable is declared within ( ) parenthesis...
The output is...
This value is passed as an argument to the function.
Multiple arguments can be passed and are separated by commas...
The output is...
value1
value2
This is another example...
The output is...
72
Arguments passed to a function are automatically stored in an array called arguments[], available only within the function. Argument names in functions do not need to be declared when passing arguments because the arguments are automatically available in the arguments[] array, which can receive an unlimited number of arguments.
A user function can return a value. The return command calls up the value...
The output is...
Hello universe!
This is another example...
The output is...
9