Javascript-Lesson 3
Operators
Comparison Operators
Comparison operators are used to test if a statement is true or false. Assume in the table that x = 6;
Operator | Description | Example |
---|---|---|
== | is equal to | x = 4 is false |
=== | is exactly equal to value and type |
x = 6 is true x = '6' is false |
!= | is not equal | x != 10 is true |
> | is greater than | x > 12 is false |
< | is less than | x < 12 is true |
>= | is greater than or equal to | x >= 12 is false |
<= | is less than or equal to | x <= 12 is true |
Logical Operators
In the table below assume that x = 10; and y = 5;
Operator | Description | Example |
---|---|---|
&& | and | (x < 8 && y > 2) is true |
|| | or | (x == 7 || y == 7) is false |
! | not | !( x == y) is true |
if statement
The syntax for the 'if' statement is:
if (condition){
code to be executed if the condition is true
}
if...else statement
The syntax for 'if..else' statement is:
if (condition){
code to be executed if the condition is true
}
else
{
code to be executed if the condition is false
}