- Part 1: Creating and Using Variables
- Part 2: Data Types and Arrays
- Part 3: Functions
- Part 4: Objects
- Part 5: Document Object Model
- Part 6: Conditional Statements
- Part 7: Loops
In previous examples, this was setting or retrieving values. With calling functions, values could be computed or changed and then returned. Each line did something. However, sometimes, lines are conditional and affect if other sections of code will be run.
“If statements,” like other forms of conditional statements, can be thought of like the pattern for using functions. There is a section of code within curly brackets that is only run if something else happens. In the case of functions, this is if it is called. For conditional statements, this is if its conditions are true.
Conditional statements test to see if some condition is true or false. If it is true, the conditional statement, in the case of “if statements,” runs some section of code. If the condition is false, the code is skipped.
If and Else Statements
Type of Conditions
So far, the type of conditional operator shown was the equal (==) one. However, there are several that can also be combined to created complex conditional statements.
- EqualWritten with two equal signs (==), the equal operator tests if two values are the same value. This means that the number four (4) and the string four (“4”) could be equal.
- Strictly Equal
Written with three equal signs (===), the strictly equal operator tests if two values are the same value AND data type. This means that the number four (4) and the string four (“4”) are not equal. However, two values that are both the number four (4) would be.
- Not EqualWritten with an exclamation mark and equal sign (!=), the not equal operators tests if two values are not the same value. This means that the number five (5) and the string four (“4”) would not be equal.
- Strictly Not EqualWritten with an exclamation mark and two equal signs (!==), the strictly not equal operators tests if two values are not the same value AND not the same data type. This means that the number five (5) and the string four (“4”) would not be equal because they are both different values AND different data types.
- Less ThanWritten with a less than sign (<), the less-than operator tests to see if the left-hand value is less than the value on the right-hand side. The less-than operator does not test if values are the same data type.
- Less Than Or EqualWritten with a less than or equal sign (<=), the less-than-or-equal operator tests to see if the left-hand value is less than OR equal to the value on the right-hand side. The less-than-or-equal operator does not test if values are the same data type.
- Greater ThanWritten with a greater than sign (>), the greater-than operator tests to see if the left-hand value is greater than the value on the right-hand side. The greater-than operator does not test if values are the same data type.
- Greater Than Or Equal
Written with a greater than or equal sign (>=), the greater-than-or-equal operator tests to see if the left-hand value is greater than OR equal to the value on the right-hand side. The greater-than-or-equal operator does not test if values are the same data type.
Advanced Condition Operators
- NotThe not operator is written as an exclamation point (!) and goes in front of some value. When that value (or something that results in a value) would be true, it negates it to false. When that value would have been false, it negates it to true.
- AndWritten with two ampersand symbols (&&), the And operator tests to see if all values (or the values as a result of some code) are ALL true. If so, the condition is also true.
- OrWritten with two bar symbols (||), the Or operator tests to see if at least one value (or the value as a result of some code) is true. If at least one value is true, the condition is true.