Ticker

6/recent/ticker-posts

JavaScript (Operators)

JAVASCRIPT OPERATORS
In java script, operators can be classify in the following three categories.



ARITHMETIC OPERATORS

Operator
Function
Example
+
Addition
C=A+B
-
Subtraction
C=A-B
*
Multiplication
C=A*B
/
Division
C=A/B

ARITHMETIC OPERATOR EXAMPLES
<form name="f">
<input type="button" value="Addittion" onclick="sum()">
<input type="button" value="Subtraction" onclick="sub()">
<input type="button" value="Multiplication" onclick="mul()">
<input type="button" value="Division" onclick="div()">
</form>

<script>
var a,b;
a=20, b=15;
function sum()
{
document.write("Sum is:",(a+b));
}

function sub()
{
document.write("Subtraction is:",(a-b));
}

function mul()
{
document.write("Multiplication is:",(a*b));
}

function div()
{
document.write("Division is:",(a/b));
}
</script>


LOGICAL OPERATORS
Operator
Function
Example
&&
And
A > B && B > C
||
Or
A >33 B || A == 33
!
Not
A != 0



LOGICAL OPERATOR EXAMPLES
<script>
var a,b;
a=20, b=15, c=17;

if(a>b && b>c)
document.write("Largest value is:",a);

else if(b>a && a>c)
document.write("Largest value is:",b);

else
document.write("Largest value is:",c);
<script>



RELATIONAL OPERATORS
Operator
Function
Example
Less than
A < B
<=
Less than or Equal to
A <= B
Greater than
A > B
>=
Greater than or Equal to
A >= B
=
Assignment
A = B
==
Comparison
A == B


RELATIONAL OPERATOR EXAMPLES


EXAMPLE 1: Program to find result by marks.
<script>
var marks;
marks=65;

if(marks >= 33)
document.write("You have pass");
else
document.write("You have failed");
<script>


EXAMPLE 2: Program to find odd/even number
<script>
var n;
n=6;

if(n%33 == 0)
document.write("Even number");
else
document.write("Odd number");
<script>


EXAMPLE 3: Program to find print greeting message by gender.
<script>
var g='f';

if(g == 'm')
document.write("Welcome miss...");
else
document.write("Welcome mr...");
<script>






Post a Comment

0 Comments