Menu Close

JavaScript Assignment Operators

Here, in this guide, you will learn about JavaScript assignment operators and their usages. In the previous JavaScript tutorial, we have covered JavaScript functions to perform specific task.

JavaScript Assignment Operators

Assignment operators in JavaScript is used to assign the value to the JavaScript variable.

OperatorExampleSame As
=x=yx=y
+=x+=yx=x+y
-=x-=yx =x-y
*=x*=yx = x * y
/=x/=yx = x / y
%=x%=yx = x % y
<<=x<<=yx = x <<= y
>>=x>>=yx >>= y
>>>=x>>>=yx >>>= y
&=x &= yx = x&y
^=x^=yx = x ^ y
|= x |=yx = x | y

JavaScript Assignment Operators Examples

Here we will see various examples of assignment operators.

The = assignment operator is used to assign the value to the variable.

<script>
var x = 100;
// Value of x is 100.
alert(x);
</script>

The += assignment operator is used to add a value to the variable.

<script>
var x = 100;
x += 100.
alert(x);
</script>

The *= assignment operator is used to multiply a value to the variable.

<script>
var x = 100;
x *= 100.
alert(x);
</script>

The -= assignment operator is used to subtract a value from the variable.

<script>
var x = 500;
x -= 100.
alert(x);
</script>

The /= assignment operator is used to divide a value to the variable.

<script>
var x = 500;
x /= 100.
alert(x);
</script>

The %= assignment operator is used to assign a remainder to the variable.

<script>
var x = 504;
x %= 100.
alert(x);
</script>

The **= assignment operator is used to return the resulting power of x**y.

<script>
var x = 2;
x **= 4.
alert(x);
</script>

Conclusion

In this article, you have learned all about JavaScript assignment operators to assign the value to the variable. Assignment operators are very helpful when you want to assign some values to the JavaScript variables.

If you like this article, please share and keep visiting for further JavaScript tutorials.

Reference:- Click Here

JavaScript Function
JavaScript Operators

Related Posts