Introduction
Assignment operators help you store or update values in variables easily.
Assignment operators help you store or update values in variables easily.
variable = value; variable += value; variable -= value; variable *= value; variable /= value; variable %= value; variable **= value;
The = operator sets a value.
Other operators like += add and assign in one step.
let score = 10; score += 5; // score is now 15
let text = 'Hi'; text += ' there!'; // text is now 'Hi there!'
let count = 20; count -= 4; // count is now 16
let total = 3; total **= 3; // total is now 27
This program shows how assignment operators change the value of points step by step.
let points = 10; console.log('Initial points:', points); points += 5; console.log('After adding 5:', points); points *= 2; console.log('After doubling:', points); points -= 4; console.log('After subtracting 4:', points); points /= 2; console.log('After dividing by 2:', points);
Assignment operators save time by combining math and assignment in one step.
Be careful with division and subtraction to avoid unexpected results.
Strings can be combined using += just like numbers.
Assignment operators store or update values in variables.
They combine math or text operations with saving the result.
Using them makes your code shorter and easier to read.
+= do in JavaScript?+= operator+= operator adds the value on the right to the variable on the left.x += y means x = x + y [OK]num by 5 and update it using an assignment operator?*=, placed between variable and value.num *= 5; which is valid syntax. Others have incorrect operator placement or invalid syntax.*= for multiply and assign [OK]let x = 10; x -= 3; console.log(x);
x starts at 10. The operator -= subtracts 3 from x.x becomes 7.x -= 3 means x = x - 3 [OK]let count = 5; count +== 2; console.log(count);
+== which is not a valid assignment operator in JavaScript.+=. So it should be count += 2;.+= not +== for addition assignment [OK]result? let result = 2; result *= 3; result += 4; result /= 2;
result = 2, after result *= 3, result becomes 6.result += 4, result becomes 6 + 4 = 10.result /= 2, result becomes 10 / 2 = 5.Pass the test and this pattern moves unknown