javaScript operators

= assign a value to a variable

the example below use the assignment operator to assign values to one or more variables.

   
  function   output(){
                         
                                var    a =40; 
                   
                                var    b ="javascript operators";
                         
                                var    c =true;
                         
                                var    x= y = z =50;             //multiple assignments         
                      
                                var    output = (a*x)+(y-z);     
                      
           document.write("the output is ____"+output);    
                       
                            } 
   output();

the assignment operator is used to assign a value to a single variable, but it is possible to perform multiple assignments at once by stringing them together with the = operator. for example :-

      var  a  =  b = c = d = 2000;  sets all fours variables to a value of 2000.

javaScript arithmetic operators

javascript arithmetic operators are used to perform arithmetic operation between variables and/or values.

the table below explains the arithmetic operators: where y=6:

operatordescriptionexampleresult of xtry it
+additionx = 5+y;11 
-subtractionx = y-42 
*multiplicationx = 4*y24 
/divisionx = y/23 
%modulus (remainder)x = y%20 
++increments a variable by onex = ++y7 
--decrements a variable by onex = --y5 

string concatenation (+) operator

the string concatenation (+) operator can also be used to add string variables or text values together.

               var  name ="javascript string";
               
               var  lastname = "operators";
               
               var  fullname = name + lastname;
               
               document.write("output value___"+fullname);
document.write("output value___"+"sandeep"+"kumar_nehra"+"  "+30);

note:if you add a number and a string, the result will be a string.

               var   x  ="10";
               
               var   y = 10;
               
               var   result = x+y;
               
               document.write("output value = "+result);
               
               document.write("<br/>type ----"+typeof(result));

javaScript assignment operators(shorthand assignment)

an assignment operator assigns a value to its left operand based on the value of its right operand.

the basic assignment operator is equal(=),other assignment operators are shorthand for the operations.

x=8 and y=2, the table below explains the assignment operators:

operatorexpanded meaningexampleoutputtry    it
= = x = yx=2 
+= x = x + y x += y x = 10 
-= x = x - y x -= y x = 6  
*= x = x * y x *= y x = 16 
/= x = x / y x /= yx = 4 
%= x = x % y x %= y x = 0 
&= x = x & yx &=yx = 0 
|= x = x | y x |= yx = 10 
^= x = x ^ y x ^= yx = 10 
<<= x = x << yx <<=yx = 32 
>>= x = x >> yx >>=yx = 2 
>>>= x = x >>> yx >>>=yx = 2 

miscellaneous operators

operatordescriptiontry   it
deletedelete object property or array value 
typeofreturns a string that identifies the data type of an expression 
voidprevents an expression from returning a value 
newcreates a new object 
intests for the existence of a property in an object 
instanceofreturns a boolean value that indicates whether or not an object is an instance of another object