Site icon i2tutorials

Javascript-Assignment

Javascript-Assignment

 

Assignment operators assign values to JavaScript variables.

 

Syntax:

 

data=value

 

Examples:

 

// Lets take some variables
x=10
y=20

x=y // Here, x is equal to 20
y=x // Here, y is equal to 10

 

There are so many assignment operator:

 

NAME                                     SHORTHAND OPERATOR       MEANING

 

Addition Assignment                           a+=b                            a=a+b

Subtraction Assignment                      a-=b                             a=a-b

Multiplication Assignment                  a*=b                            a=a*b

Division Assignment                            a/=b                             a=a/b

Remainder Assignment                      a%=b                           a=a%b

Exponentiation Assignment                a**=b                          a=a**b

Left Shift Assignment                          a<<=b                          a=a<<b

Right Shift Assignment                        a>>=b                          a=a>>b

Bitwise AND Assignment                    a&=b                           a=a&b

Bitwise OR Assignment                       a|=b                            a=a | b

Bitwise XOR Assignment                     a^=b                            a=a^b

 

 Addition Assignment::

 

Adds the value to the right operand to a variable and assigns the result to the variable.

 

Example:

 

<script>
let a = 2;
const b= 3;

// Expected output: 2
document.write(a);

document.write('</br>');

// Expected output: 4
document.write(a = b + 1);
</script>

 

OUTPUT:

 

2

4

 

Subtraction Assignment:

 

This operator subtracts the value of the right operand from a variable and assigns the result to the variable.

 

Example:

 

<script>  
let yoo=4;  
document.write(foo=yoo-1); // 4-1  
</script>

 

OUTPUT:

 

3

 

Multiplication Assignment:

 

Multiplies variables by the values of the right operand and assigns the results to the variables.

 

Example:

 

<script>  
let yoo=5;  
document.write(yoo=yoo*2); // 5*2  
</script>

 

OUTPUT:

 

10

 

Division Assignment:

 

Divides variables by the values of the right operand and assigns the results to the variables.

 

Example:

 

<script>  
 let yoo=10;
 const moo=2;  
 document.write(yoo=yoo/moo); // 10/2  
 document.write("</br>");  
 document.write(yoo/=0); // Infinity  
</script>

 

OUTPUT:

 

5

Infinity

 

Remainder Assignment:

 

Divides variables by the value of the right operand and assigns the remainder to the variables.

 

Example:

 

<script>  
let yoo=50;  
document.write(yoo%=10); //zero  
</script>

 

OUTPUT:

 

0

 

Exponentiation Assignment:

 

Raises the value of a variable to the power of the right operands.

 

Example:

 

<script>  
 let yoo=2;
 const moo=2;  
 // 2 raise to the power 2
 document.write(yoo**moo);  
</script>

 

OUTPUT:

 

4

 

Left Shift Assignment:

 

Moves the specified amount of bits to the left and assigns the results to the variables.

 

Example:

 

<script>
var yoo=5; // 101

// 20(In Binary 10100)
document.write(yoo<<=2);
</script>

 

OUTPUT:

 

20

 

Right Shift Assignment:

 

Moves the specified amount of bits to the right and assigns the results to the variables.

 

Example:

 

<script>
var yoo=5;  
document.write(yoo>>=2); // 001 
</script>

 

OUTPUT:

 

1

 

Binary AND Assignment:

 

Its representation of both operands does a bitwise AND operation on them and assigns the results to the variables.

 

Example:

 

<script>
var yoo=5; 
document.write(yoo&=2); // 000  
</script>

 

OUPUT:

 

0

 

Binary OR Assignment:

 

Its representation of both operands does a bitwise OR operation on them and assigns the results to the variables.

 

Example:

 

<script>
var yoo=5;  
// 7(In binary: 111)
document.write(yoo|=2); 
</script>

 

OUTPUT:

 

7

Exit mobile version