|
LILLY ROBOT |
JavaScript has several operators but it is important to understand the difference between operhands and operators.
butter+milk
butter and milk are called operhands.
The + sign is an arithmatic operator.
It adds up the values of the two operhands butter and milk.
| Operator | Description |
|---|---|
| + | Addition: Adds the first and second operhands |
| - | Minus: Subtracts the first operhand from the second operhand |
| * | Multiplication: Multiplies both operhands |
| / | Division: Divides the first operhand by the second operhand |
| % | Modulus: Divides the first operhand by the second and returns the remainder |
| ++ | Increment: Increases the value by one |
| -- | Decrement: decreases the value by one |
Note: In the above
example we incremented the value
of a from 10 to 11 then we decremented the value of a
from 11 to 10.
Displays
The + sign can also be used to join the values of two or more strings.
Displays
Let's have a look what happens if we use the + operator to:
Displays
As you can see in result 1 and 2 the values num2 and num3 are strings. Whereas, in result3 the num2 and num3 are numbers because they have been placed inside a bracket.
In the table below we have used the following x and y values to find the new value
of x:
x=8 y=4
| Operator | Example | Means | Answer |
|---|---|---|---|
| += | x+=y | x=x+y | 12 |
| -= | x-=y | x=x-y | 4 |
| *= | x*=y | x=x*y | 32 |
| /= | x/=y | x=x/y | 2 |
| %= | x%=y | x=x%y | 0 |
In the comparison operator a boolean value true or false is returned.
In the table below we have used the following x, y, a and b values: x=10 y=5 a="10" b="10"
| Operator | Description | Example |
|---|---|---|
| == | Means equal Compares the two operhands x and y if equal it returns true else returns false. |
x==y 10==5 returns false |
| === | Means equal value and type Compares the two operhands x and y if equal and of the same type then it returns true else returns false. |
a===b "10"==="10" returns true |
| != | Means not equal Compares the two operhands x and y if not equal it returns true else returns false. |
x!=y 10!=5 returns true |
| > | Means greater than If x is greater than y it returns a true else returns false. |
x>y 10>5 returns true |
| < | Means less than if x is less than y it returns a false else returns true |
x<y 10<5; returns false |
>= | Means greater than or equal to if x is greater than or equal to y it returns a true else returns false |
x>=y 10>=5; returns true |
| <= | Means less than or equal to if x is less than or equal to y it returns a true else returns false |
x<=y 10<=5; returns false |
In the table below we have used the following values for x and y:
x=10   y=5
| Operator | Description | Example |
|---|---|---|
| && | Means And returns a true if both operhands x and y are true else returns false |
x>4 && y<2 10>4 && 5<2 returns false |
| || | Means OR returns a true if either operhands x and y are true else returns false |
x>20 || y<7 10>20 || 5<7 returns true |
| ! | Means Not if the value of x is true then it returns an inverse value |
!(x>y) !(10>5) returns false |