Skip to main content

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. 







Comments

Popular posts from this blog

Assignment of ITA/ Information Technology and Application BCA- Technology369kk

Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

C++ and Java Practical All Questions Answers - BCA -Technology369kk

C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

Assignment of PMO (Principal of Management and Organization) - Technology369kk

 ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...