Javascript3

Operators

Arithmetic Operators

Arithmetic operators are used in daily life, like adding , subtracting, multiplying , dividing etc. The arithmetic operators are supported by JavaScript are :

Operator Description
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
Decrement

Example

<html>
<body>
<script type="text/javascript">

var a=40; var b = 50; var c= 5;
var d; d = a+b document.write("<br> Value of a is : "+a);
document.write("<br> Value of b is : "+b);
document.write("<br> Value of c is : "+c);
document.write("<br>Sum of a & b is : "+d);
d=b-c; document.write("<br>Difference of b & c is : "+d);
d= a*c;
document.write("<br>Multiplication of a & c is : "+d);
d=b/c;
document.write("<br>Quotient of Division of b & c is : "+d);
d = a%c; document.write("<br>Remainder of Division of a & c is : "+d);
a++; b—;
document.write(“<br>Value of A is now “+a);
document.write(“<br>Value of b is now “+b);

</script >
</body>
</html>

Understanding the program:
Variable d store the result of every arithmetic operation with the two operands and displays the result. Division operator (/) returns the quotient of the division like 40/5= 8 but modulus (%) returns the remainder of the division like 40%5 = 0.the value of a will get increment with one a++ is equal to a=a+1 and b— is equal to b = b-1. so the value of a was 40 it got incremented with one and became 41 while the value of b was 50 and got decremented with one and became 49.

Output is:

 Value of a is: 40 Value of b is: 50 Value of c is: 5 Sum of a & b is: 90 Difference of b & c is: 45 Multiplication of a & c is: 200 Quotient of Division of b & c is: 10 Remainder of Division of a & c is: 0 Value of A is now 41 Value of b is now 49

Top

Logical Operators

Logical operators are used to perform Boolean operations on operands. Logical operators are and (&&), or ( || ) , not (!) . The value returned after using a logical operator is Boolean value true or false. Logical operators are connectors of expressions also.

Operator Description
&& Both the expression should be correct then it returns true else it returns false. It is known as “and” operator
|| If Any of the given expressions is found true then it returns true else it returns false.
! If the given expression is true it returns false and if given expression is false it returns true.

Example

<html>
<body>
<script>

var a=9
var b=8
var c=25
document.write("<br> a is : "+a)
document.write("<br> a is : "+b)
document.write("<br> a is : "+c)
document.write("<br>is a greater than b and greater c also ? : "+(a>b && a>c)) document.write("<br>is a greater than b or greater c ? : "+(a>b || a>c)) document.write("<br>not c is greater than a ? : "+!(c>a))

</script>
</body>
</html>

Output:

 a is : 9 a is : 8 a is : 25 is a greater than b and greater c also ? : false is a greater than b or greater c ? : true not c is greater than a ? : false

Top

Assignment Operators

As the name suggest these operators are used to assign values to variables. The right hand value or variable is assigned to the left hand variable. Different assignment operators are listed below with these use.

Operator Description Example
= This operator is used to assign value of variable/value only at right hand of it to the variable at left hand of this operator. A=5 B=a C=a+b
+= Increments the left hand variable with the right hand variable a+=b or a=a+b
-= Decrements the left hand variable with the right hand variable a-=b or a=a-b
*= Multiplies the left hand variable with the right hand variable a*=b or a=a*b
/= Divides the left hand variable with the right hand variable a/=b or a=a/b
%= Keeps the remainder of division of left hand variable by right hand variable in the left hand variable a%=b or a=a%b

Example

<html>
<body>
<script type="text/javascript">

var a,b
a=5
b = 6
document.write("<br>Value of a : "+a);
document.write("<br>Value of b : "+b);
a+=b;
document.write("<br>Value of a after a+=b : "+a);
a-=b
document.write("<br>Value of a after a-=b : "+a);
a*=b
document.write("<br>Value of a after a*=b : "+a);
a/=b
document.write("<br>Value of a after a/=b : "+a);
a%=b
document.write("<br>Value of a after a%=b : "+a);

</script>
</body>
</html>

Output:

 Value of a : 5 Value of b : 6 Value of a after a+=b : 11 Value of a after a-=b : 5 Value of a after a*=b : 30 Value of a after a/=b : 5 Value of a after a%=b : 5

Top

Comparison Operators

As the name suggest these operators are used to compare two variable values. The left hand variable is compared with the right hand operator using these operators. The answer returned after use of these expressions is a Boolean value which tell that whether comparisons is true or false. A programmer can program for either situation. Different comparison operators are listed below.

Operator Description Example
== This operator is used to compare value or variable at right hand of it to the variable at left hand of this operator. A==b 5==9 (returns false)
=== This three times equal sign is called strictly equal sign it checks for values as well type of datas contained by the variables at left hand and right hand A=90 B=90 C=”90” A===B will return true because value of A & B is 90 and both are number A===C will return false because value of A & C is 90 but types are different
!= This operator is called not equal it checks whether the left and right operands are not equal then it returns true else returns false A=50 B=90 C=50 A!=B will return true A!=C will return false
This operator is known as greater than if the left operand is greater than right operand then returns true else return false A=45 B=15 C=90 a>b will return true a>c will return false
This operator is known as less than if the left operand is less than right operand then returns true else return false A=45 B=15 C=90 A<b will return false A<c will return true
>= This operator is known as greater than equal to if the left hand operand is greater than or equal to right hand operand then it returns true else false A=45 B=95 C=45 A>=b will return false A>=c will return true because both are equal.
<= This operator is known as less than equal to if the left han operand is lesser than or equal to the right hand operand then it returns true else it returns false. A=45 B=95 C=45 A<=b will return true A<=c will return true because both are equal.

Example

<html>
<body>
<script type="text/javascript">

var a,b,c; var d;
a=5
b = 6
c=4
d="6"
document.write("<br>Value of a : "+a);
document.write("<br>Value of b : "+b);
document.write("<br>Value of c : "+c);
document.write("<br>Value of d : "+d);
document.write("<br>Result of a==b : "+(a==b));
document.write("<br>Result of a===b : "+(a===b));
document.write("<br>Result of b===d : "+(b===d));
document.write("<br>Result of a!=b : "+(a!=b))
document.write("<br>Result of a>=b : " +(a>=b));
document.write("<br>Result of a<=b : " +(a<=b));
document.write("<br>Result of (a>d) : " + (a>d));
document.write("<br>Result of (b<=c) : " + (b<c));

</script>
</body>
</html>

Output:

 Value of a : 5 Value of b : 6 Value of c : 4 Value of d : 6 Result of a==b : false Result of a===b : false Result of b===d : false Result of a!=b : true Result of a>=b : false Result of a<=b : true Result of (a>d) : false Result of (b<=c) : false

Understanding the program:
In above program, we have used all the operators described and the result is also displayed from which we can understand the functioning of comparison operators.
Top

String Operators

A string operator joins two string values of two string variables and creates a third string value. String operators concatenates two strings. Even the value is numeric in a string variable it is also concatenated.

Like “20” + “30”
the answer will be 2030, not 50.

Operator Description Example
+ This operator joins the string at left hand side with the string at right hand side A=”India” B=”Gate” C=A+B , C is holding a value IndiaGate

Example

<html>
<body>
<script type="text/javascript">

var a=" India "
var b = "Gate"
var c= a+b
document.write("<br>Value of a "+a);
document.write("<br>Value of b "+b);
document.write("<br>Value of c "+c);

</script >
</body>
</html>

Output:

 Value of a India Value of b Gate Value of c IndiaGate

Top

Ternary Operators

Ternary operator require one expression, on variable or value for true case and one variable or value for false case. If the condition in given expression is found true it assigns value to variable present in true value position else the value of false value position will be assigned.

 variable = condition ? true value : false value

Variable to which you want to assign value = condition ? in case of condition is true this value will be assigned : in case of condition is false this value will be assigned
like:
var p=90
var a = p>100 ? 25:50

p is having value 90 we are comparing that whether p is greater than 100 which is false because p is 90 so the value 50 will be assigned to a. If p =150 then value 25 will be assigned to a.

Example

<body>
<script type="text/javascript">

var a=50
var b b= a>=50 ? "Greater then or equal to 50 " : "Lesser than 50" document.write("<br>Value of a "+a);
document.write("<br>Value of b "+b);

</script >
</body>

Understanding program :
value of a is 50 we are checking that if a is greater than or equal to 50 then “Greater than or equal to 50” should be assigned to it else “Lesser than 50” should be assigned to variable b . here value of a is 50 so the condition becomes true and “Greater than or equal to 50” will be placed in variable b.

Output:

 Value of a 50 Value of b Greater then or equal to 50