Learn JavaScript Assignment






In Javascript we use Assignment Operators to assign values to variables



Operator Example Also equal to
+= x += y x = x + y
= x = y x = y
*= x *= y x = x * y
-= x -= y x = x - y
/= x /= y x = x / y
**= x **= y x = x ** y
%= x %= y x = x % y
&= x &= y x = x & y
<<= x <<= y x = x << y
>>= x >>= y x = x >> y
>>>= x >>>= y x = x >>> y
^= x ^= y x = x ^ y
|= x |= y x = x | y


Assignment Examples

assignment operator =

Code Example:

                    var a = 5; 
                   //comment:  assigns 5 to a variable.
                 
                 
Try yourself >>





assignment operator -=

Code Example:

                    var a = 20;
a -= 10; 
//comment subtracts a value of 10 from a variable.
                 
                 
Try yourself >>





assignment operator +=

Code Example:

                    var a = 20;
a += 5;
 //comment:  adds a value of 5 to a variable(a).
                 
                 
Try yourself >>





assignment operator *=

Code Example:

                    var a = 10;
x *= 2; 
                   //comment:  multiplies a variable.
                 
                 
Try yourself >>

assignment operator /=

Code Example:

                    var a = 10;
a /= 2; 
                   //comment: divides a variable.
                 
                 
Try yourself >>

assignment operator %=

Code Example:

                    var a = 10;
a %= 5; 
                   //comment: assigns a remainder to a variable.
                 
                 
Try yourself >>













Learn Javascript and other Web development classes for free, our main goal is to allow students to learn to code easily and free



Navigation