In Association with Amazon.in   Flipkart

Tuesday, July 9, 2013

Operators In Javascript With Examples

Operators in Javascript
  1. Arithmetic
  2. Relational
  3. Logical
  4. Concatenation
  5. Assignment
  6. Inc / Dec
  7. Ternary

 1. Arithmetic

            +  Addition
            -   Subtraction
            *   Product
            /    Division
            %  Modulus        (5%2 = 1)



     a = a+b
     a + = b

     a = a-b
     a - = b

     a = a*b
     a * = b

     a = a / b
     a / = b

     a = a % b
     a % = b

2. Relational

==     Equals to
!=      Not equals to
>       Greater then
<       Less then
>=     Greater then or Equals to
<=     Less then or Equals to

3. Logical
---------
And            &&
Or              ||
Not             !

If (a==10 && b==20)
{
.....
.....
}

4. Concatenation

+                 Addition (if both are numbers)
                  Concatenation (if any one is string)
 

5. Assignment
a = 10 ;
a = b ;

6. Incrementation/ Decrementation

                      ++              incrementation
                      --               Decrementation

Ex :
                     
                      a = 10 ;

                      a++;   similar to ( a + = 1)  that is    a = a + 1 => a=11

                      a-- ;    similar to ( a - = 1)  that is    a = a - 1 => a=9

Post increment

          document.write (a++) ;

Pre increment

            document.write (++a) ;

        i=5;
        j=i++;  //  post increment          => j=5, i=6
        k=5;
        p=++k;  //  pre increment          => p=6, k=6

 

Note :
Unary operator takes one Value .   (  !  ,  ++  ,  --  )
Remaining all operators are Binary Operators  ( it takes 2 values )

7. Ternary operator
--------------------
Var = ( condition ) ?   true val 1  :   false val 2

Example

<html>
<head>
           <script>
          
           a = 10;
           b = 20;
           c = ( a > b ) ? a : b;          
           document.write( c );
          
           </script>
</head>
<body>
</body>
</html>

No comments:

Post a Comment


Related Posts Plugin for WordPress, Blogger...