Loading, please wait...

MCQ

What are Operators in JavaScript ?

Aug 29, 2020 JavaScript, 3013 Views
Operators in JavaScript with example

What are JavaScript Operator

Operators are symbols that add expression in a statement, which could have Arithmetic expression, Relational Expression, Comparision Expression, Conditional Expression, or even Assignment Expression. These expressions not only add meaning to a statement but also make a statement understandable.

For example:

6 4 10

The above statement without any expression or operators seems like some series or non-relatable, but,

6+4=10

The above statement clearly depicts the behavior change as soon as an operator is added to the statement, thus defining the need for operators.

Lets now move on to the operators in JavaScript:

  • Arithmetic Operators
  • Relational Operators
  • Comparison Operators
  • Conditional Operators
  • Assignment Operators

Let's Understand Arithmetic Operator with an example

<html>
   <body>
   
      <script type = "text/javascript">
         <!--
            var a = 25;
            var b = 22;
            var c = "Statement";
            var linebreak = "<br />";
         
            document.write("a + b = ");
            result = a + b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a - b = ");
            result = a - b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a / b = ");
            result = a / b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a % b = ");
            result = a % b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a + b + c = ");
            result = a + b + c;
            document.write(result);
            document.write(linebreak);
 
         //-->
      </script>
      
   </body>
</html>
         
         

Let's understand Relational Operators through an example

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var A1 = true;
            var B2 = false;
            var linebreak = "<br />";
      
            document.write("(A1 && B2) => ");
            result = (A1 && B2);
            document.write(result);
            document.write(linebreak);
         
            document.write("(A1 || B2) => ");
            result = (A1 || B2);
            document.write(result);
            document.write(linebreak);
         
            document.write("!(A1 && B2) => ");
            result = (!(A1 && B2));
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
   </body>
</html>

Let's understand Comparision Operator with an example

<html>
   <body>  
      <script type = "text/javascript">
         <!--
            var a = 64;
            var b = 46;
            var linebreak = "<br />";
      
            document.write("(a == b) => ");
            result = (a == b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a < b) => ");
            result = (a < b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a > b) => ");
            result = (a > b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a != b) => ");
            result = (a != b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a >= b) => ");
            result = (a >= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a <= b) => ");
            result = (a <= b);
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
   </body>
</html>

Let's understand the Conditional Operator with an example

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = 445;
            var b = 980;
            var linebreak = "<br />";
         
            document.write ("((a > b) ? 600 : 400) => ");
            result = (a > b) ? 600 : 400;
            document.write(result);
            document.write(linebreak);
         
            document.write ("((a < b) ? 600 : 400) => ");
            result = (a < b) ? 600 : 400;
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
   </body>
</html>

Let's understand the Assignment Operator with the help of an example

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = 65;
            var b = 45;
            var linebreak = "<br />";
         
            document.write("The Value of a => (a = b) => ");
            result = (a = b);
            document.write(result);
            document.write(linebreak);
         
            document.write("Value of a => (a += b) => ");
            result = (a += b);
            document.write(result);
            document.write(linebreak);
         
            document.write("The Value of a => (a -= b) => ");
            result = (a -= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("The Value of a => (a *= b) => ");
            result = (a *= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("The Value of a => (a /= b) => ");
            result = (a /= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("The Value of a => (a %= b) => ");
            result = (a %= b);
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
   </body>
</html>
MCQ

Related Article