BEST SITE FOR WEB DEVELOPERS

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Dates JS Arrays JS Sets JS Maps JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Events JS Programming JS References JS UTF-8 Characters

JS Advanced

JS Versions JS Functions JS Objects JS Classes JS Iterations JS Promises JS Modules JS HTML DOM JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Objects

JavaScript. W3Schools in English. Lessons for beginners

Ua Es De Fr Ru

JavaScript Assignment


JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.

Operator Example Same As
= 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
**= x **= y x = x ** y

The **= operator is a part of ECMAScript 2016.


Assignment Examples

The = assignment operator assigns a value to a variable.

Assignment

let x = 10;
Try it Yourself »

The += assignment operator adds a value to a variable.

Assignment

let x = 10;
x += 5;
Try it Yourself »

The -= assignment operator subtracts a value from a variable.

Assignment

let x = 10;
x -= 5;
Try it Yourself »

The *= assignment operator multiplies a variable.

Assignment

let x = 10;
x *= 5;
Try it Yourself »

The /= assignment divides a variable.

Assignment

let x = 10;
x /= 5;
Try it Yourself »

The %= assignment operator assigns a remainder to a variable.

Assignment

let x = 10;
x %= 5;
Try it Yourself »

Test Yourself With Exercises

Exercise:

Use the correct assignment operator that will result in x being 15 (same as x = x + y).

    x = 10;
    y = 5;
    x  y;