0
0
Javascriptprogramming~5 mins

Assignment operators in Javascript

Choose your learning style9 modes available
Introduction

Assignment operators help you store or update values in variables easily.

When you want to save a value in a box (variable).
When you want to add or subtract a number from what is already saved.
When you want to multiply or divide a value and keep the result in the same box.
When you want to combine text and save it back to the same variable.
When you want to quickly update a variable without writing it twice.
Syntax
Javascript
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.

Examples
Add 5 to score and save it back to score.
Javascript
let score = 10;
score += 5;  // score is now 15
Combine strings and save the result.
Javascript
let text = 'Hi';
text += ' there!';  // text is now 'Hi there!'
Subtract 4 from count and update it.
Javascript
let count = 20;
count -= 4;  // count is now 16
Raise total to the power of 3 and save it.
Javascript
let total = 3;
total **= 3;  // total is now 27
Sample Program

This program shows how assignment operators change the value of points step by step.

Javascript
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);
OutputSuccess
Important Notes

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.

Summary

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.