Operators

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.





Arithmatic Operators


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




Example

<script>
var a=10;
var b= 4;

document.write("a+b=" + (a+b) + "<br><br>");
document.write("a-b=" + (a-b) + "<br><br>");
document.write("a*b=" + (a*b) + "<br><br>");
document.write("a%b=" + (a%b) + "<br><br>");
document.write("a/b=" + (a/b) + "<br><br>");
document.write("++a is" + (++a) + "<br><br>");
document.write("--a is" + (--a) + "<br><br>");
< script>

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




Concatenation

The + sign can also be used to join the values of two or more strings.

< script>
var question="What is 5% of 10? ";
var ans="Answer:0.5";
document.write(question + "" + ans);
</script>

Displays


Let's have a look what happens if we use the + operator to:

  1. add a string value with a number
  2. add two numbers without brackets
  3. add two numbers with brackets

<script>
var room="Number:";
var num2=3;
var num3=9;

document.write("result 1=" + room+num2);
document.write("result 2=" + num2+num3);
document.write("result 3=" +(num2+num3));
<script>

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.




Assignment Operators

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




Comparison Operators

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




Logical Operator

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



Now do the Exercise!