Chapter 6 Operators in JavaScript - Technology369kk

 Operators

Now discuss about operators in JavaScript, this is most important chapter for every programmer. So lets us take a simple expression 3+5 is equal to 8. So here 3 and 5 are called operand and "+"  is called operator. Be careful this is most important line for this chapter. 

JavaScript supports the following types of operators. 

Lets have a look at all the operators one by one: 

Arithmetic Operators: 

JavaScript the following arithmetic operators : 

 
    var a =10;
        var b =20;

  • Assume variable A holds 10 and varaibles B holds 20, then : 
  • Lets see the following on Code
 

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

    resultAdd= a + b;
    document.write("a + b =",resultAdd,"</br>");
   
    resultSub= a - b;
    document.write("a - b =",resultSub,"</br>");
   
    resultMul= a * b;
    document.write("a * b =",resultMul,"</br>");
   
    resultDiv= a / b;
    document.write("a / b =",resultDiv,"</br>");
 
    resultMod= a % b;
    document.write("a % b =",resultMod,"</br>");
 
    resultIncr= a++;
    document.write("a ++ =",resultIncr,"</br>");

    resultDecr= b--;
    document.write("b-- =",resultDecr,"</br>");

</script>

<!-- Input The Diffrent Value and Check the results -->



Comparison Operators

JavaScript also support the following comparison operators : 
  • Assume variable A holds 10 and varaibles B holds 20, then : 

 
    var a =10;
       var  b =20;



Let's  see an example :  

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chapter 6 Operators</title>
</head>

<body>
   
<script>
    document.write("Comparison Operators </br>");  // br is used for just new line

    var a= 10;
    var b= 20;

   document.write( "a is store = 10, b is also stored 20 after that:</br> ");

    document.write("(a == b) Equal To =>" ,a == b,"  </br>");
    document.write("(a != b) Not Equal To=>" ,a != b,"  </br>");
    document.write("(a >  b)  Greater than=>" ,a > b,"  </br>");
    document.write("(a <  b)  Less than=>" ,a < b,"  </br>");
    document.write("(a >=  b)  Greater than and Equals to=>" ,a >= b,"  </br>");
    document.write("(a <=  b)  Less than and Equals to=>" ,a <= b,"  </br>");

 
</script>

<!-- Input The Diffrent Value and Check the results -->



</body>

</html>



Logical/Relational Operators:

JavaScript also provided like another language Logical Operators: 
  • Assume Variable a=10 and b = 20 after that, 
Let's  see an example: 

 document.write("Logical Operators </br>");  // br is used for just new line
    var a= "true";
    var b= "false";
   
   
    document.write( "a is store = 10, b is also stored 20 after that:</br> ");
// U can just try to practical for this.

    document.write("(a && b) is results==>>",a && b, "</br>");
    document.write("(a != b) is results==>>",a != b,"</br>");
    document.write("(a !&& b) is results==>>", (!(a && b)),"</br>");
   

Ans:  

a is store = 10, b is also stored 20 after that:
(a && b) is results==>>false
(a != b) is results==>>true
(a !&& b) is results==>>false


Bitwise Opeator : 

  • JavaScript  supports the following bitwise operators:  
  • Assume variable A holds 2 and varaible B holds 3 then, 
  • Let's see an example of all:  
 
<script>
    document.write("Bitwise Operators </br>");  // br is used for just new line
    var x = 2;  // bit presentation 10
    var y = 3;  // bit presentation 11

    document.write("(x & y) =>",(x & y),"</br>");
    document.write("(x | y) =>",(x | y),"</br>");
    document.write("(x ^ y) =>",(x ^ y),"</br>");
    document.write("(~ y) =>",(~y),"</br>");
    document.write("(x << y) =>",(x << y),"</br>");
    document.write("(x >> y) =>",(x >> y),"</br>");
    document.write("Set the variables to different values and different operators and then try... </br>" );
</script>


  • OUTPUT:

Bitwise Operators

(x & y) =>2
(x | y) =>3
(x ^ y) =>1
(~ y) =>-4
(x << y) =>16
(x >> y) =>0
Set the variables to different values and different operators and then try...


Assignment Operators

  • JavaScript  supports the following assignment operators:  

  • SameLogic appliles to bitwise operators so  they will become <<=, >>=, ^= , &= , |= and ^=.  
  • Let's see an example : 

<script>
    document.write("</br> Assigment Operators");
    var a=11;
    var b=22;
    // resultAss= (a = b);
    document.write("</br> Value of a => Simple Assignment: =>>",(a = b));
    document.write("</br> Value of a => Add and Assignment: =>>",(a += b));
    document.write("</br> Value of a => Sub and Assignment: =>>",(a -= b));
    document.write("</br> Value of a => Mul and Assignment: =>>",(a *= b));
    document.write("</br> Value of a => Div and Assignment: =>>",(a /= b));
    document.write("</br> Value of a => Mod and Assignment: =>>",(a %= b));
   
    // <!-- Input The Diffrent Value and Check the results -->

</script>

OUTPUT:


    document.write("</br> Assigment Operators");
    var a=11;
    var b=22;
    // resultAss= (a = b);
    document.write("</br> Value of a => Simple Assignment: =>>",(a = b));
    document.write("</br> Value of a => Add and Assignment: =>>",(a += b));
    document.write("</br> Value of a => Sub and Assignment: =>>",(a -= b));
    document.write("</br> Value of a => Mul and Assignment: =>>",(a *= b));
    document.write("</br> Value of a => Div and Assignment: =>>",(a /= b));
    document.write("</br> Value of a => Mod and Assignment: =>>",(a %= b));
   
    // <!-- Input The Diffrent Value and Check the results -->

</script>

OUTPUT: 

Assigment Operators
Value of a => Simple Assignment: =>>22
Value of a => Add and Assignment: =>>44
Value of a => Sub and Assignment: =>>22
Value of a => Mul and Assignment: =>>484
Value of a => Div and Assignment: =>>22
Value of a => Mod and Assignment: =>>0


Miscellaneous Operators:  

  • We will discuss two operators here that quite useful in JavaScript: 
  •  the Conditional Operator ( ?: ) and the typeof operator.

Conditional/ Ternary Operators:  

  • The conditional operator first evalutes an expression for a true or false value and then excutes one of the two given statements depending upon the results of the evaluation.
  • ?: (Conditional)  : IF condition is true?  Then Value X : Otherwise value Y

<script>
    document.write(" </br>Conditinal Operators </br>");

    var p= 10;
    var q= 20;
    document.write("</br> ((a>b)? 100 : 200) => ");
    resultCon = (a> b) ? 100: 200;   // grater than
    document.write(resultCon);


    document.write(" </br> ((a>b)? 100 : 200) =>");
    resultCon = (a < b) ? 100: 200;   // Less than
    document.write(resultCon);


</script>



TypeOf Operators: 

  • The typeof operator is a unary operator that is placed before its single operand , which can be of any type. Its value is a string indicating the data type of the operand. 
  • The typeof operator evalutes to "number", string or "boolean"  if its operand is a number, string or boolean value and return true or false based on the evalution. 
  • Here is a list of the return values for the typeof Operator.

    <script>
    document.write(" </br>  </br> Typeof Operator");

    var t= 10;
    var s="String";
    resultToF = (typeof  s == "string" ? " S is String" : " S is Numric");
    document.write(" </br>Print Result :",resultToF);


    resultToF = (typeof  t == "string" ? "  T is String" : " T is Numric");
    document.write(" </br>Print Result :",resultToF);

   
    </script>



Output :

((a>b)? 100 : 200) => 200
((a>b)? 100 : 200) =>100

Typeof Operator
Print Result : S is String
Print Result : T is Numric

 I hope this is helpful  for you, Thanks for watching. 







Post a Comment

0 Comments