Home > DeveloperSection > Beginner > Implementing Operators in Java Script

Implementing Operators in Java Script


JavaScript JavaScript 
Ratings:
0 Comment(s)
 4920  View(s)
Rate this:

Implementing Operators in Java Script

We can use arithmetic operators in java script to perform mathematical calculation or used to solve mathematical expression.

Types of operators in Java Script

Ø  Arithmetic Operator (+,-,*, %,/) à Arithmetic operator in java script are used for mathematical operation.” +” Operator is used for addition purpose, “-“operator are used for subtraction purpose, “*” operator are used for multiplication purpose, “% “pronounce as modulo operator are used to retrieve remainder purpose and” /” divide operator are used for division purpose. The entire arithmetic operator works on two operands. If both operands are integer then resultant is also integer and if both are float then resultant operand is also float.

Following example demonstrate use of Arithmetic operator in java script

<script type="text/javascript">

        function operatorDemo() {

            document.writeln("Arithmatic operation demo.<br />");

            var num1 = 45, num2 = 45;

            document.writeln("Addition of two number is   :" + (num1 + num2) + "<br />");

            document.writeln("Subtraction of two number is   :" + (num1 - num2) + "<br />");

            document.writeln("Multiplication of two number is   :" + (num1 * num2) + "<br />");

            document.writeln("Division of two number is   :" + (num1 / num2) + "<br />");

            document.writeln("Remainder of two number is   :"+(num1 % num2) + "<br />");

        }

</script>

Output of the following code snippet is as follows

Implementing Operators in Java Script

Ø  Relational Operator (<, >, ==)à Relational operator in java script are used for comparison purpose. Whenever you want to compare two objects for their equality then we use relational operator. “==” Operator is used to test two objects for equality. “<” Operators check whether one value is less than other value and “>” operators check whether one number is greater than other or not.

Following example demonstrate use of relational operator in java script

function operatorDemo() {

            document.writeln("Relational operatior demo.<br />");

            var num1 = 45, num2 = 60;

            document.write("Is num1 is equal to num2   :&nbsp;&nbsp;" + (num1 == num2) + "<br />");

            document.write("Is num1 less than num2   :&nbsp;&nbsp;" + (num1 < num2) + "<br />");

            document.write("Is num1 greater than num2   :&nbsp;&nbsp;" + (num1 > num2) + "<br />");

}

Output of the following code snippet is as follows

Implementing Operators in Java Script

True values represents that relational operator that we implement evaluates to true and false value represents that above relational operator evaluates to false.

Ø  Logical Operator (&&, ||,!) àWe can use logical operator in java script to combined more than one expression. There is two types of logical operator one is and (&&) and another one is or (||). and operator evaluates to true is both expression passed to and operator is true otherwise it always return false while or operator evaluates false only when both expression which is passed to or operator is evaluates to false otherwise return true. ! (not) is known as negation operators which reverse the result of Boolean expression. It also reverses the meaning of relational operator.

The following example shows the demonstration of Logical operator

function operatorDemo() {

            document.writeln("Logical operator demo.<br />");

            var num1 = 45, num2 = 60;

            var res = ((num1 != num2) && (num1 < num2));

            document.write("Output of and (&&) logical operator is  :&nbsp;&nbsp;" + res + "<br />");

            res = ((num1 == num2) || (num1 > num2));

            document.write("Output of or (||) logical operator is  :&nbsp;&nbsp;" + res);

}

Output of the above code snippet is as follows

Implementing Operators in Java Script


Don't want to miss updates? Please click the below button!

Follow MindStick