Last updated on September 14, 2023
In JavaScript, assignment operators are used to give values to variables, often with numbers. These operators are important for things like if statements, else statements, and loops.
The equal sign (=) is the most common operator in JavaScript. Below is the list of the assignment operators.
Operator | Syntax | Example | Definition |
= | y = z | let y = 10; | Equal the numeric value |
+= | y += z | let y += z; | Add the numeric values to each other. |
-= | y -= z | let y -= z; | Subtract the numeric values from each other. |
*= | y *= z | let y *= z; | Multiply the numeric values on each other. |
/= | y /= z | let y /= z; | Divide the numeric values on each other. |
%= | y %= z | let y %= z; | Get the Reminder |
**= | y **= z | let y **= z; | Get the exponential value. |
The = operator, also known as the simple assignment operator, is used to assign a value to a variable
Look at the below example in which the variable y is equal to 80. then we put the variable in console.log for printing. It will return us 80.
let y = 80;
console.log(y);
// output: 80
The addition assignment operator (+=) adds a value to an existing variable. It is used for integer and float data types.
In the below example, the y has assigned value 10 in which we want to add 5. To do that we used addition assignment operator.
let y = 10;
y += 5;
console.log(y);
//output: 15
The subtraction operator subtracts the specified value from another variable’s value. It is used for both integer and float data types.
let y = 20;
y -= 5;
console.log(y);
// output: 15
In this example, we have a variable y that has 20 value assigned. we want to take away 5 from that number. We use a special sign ‘-=’ to do this. So, after we do that, ‘y’ becomes 15.
The multiplication operator multiplies the value from another value that you put. It is used for integer and float data types.
The variable let y is 6 and we multiply it by 3 then we put the variable in console.log for printing. It will return us 18.
let y = 6;
y *= 3;
console.log(y);
// output: 18
In the above example, we equal the variable let y to 6 and then multiply with 3 by the assignment operator multiplication after that use console.log for printing.
The division operator divides the value from another value that you put. It is used for integer and float data types.
The variable let y is 30 and we divide it by 6 then we put the variable in console.log for printing. It will return us 5.
let y = 30;
y /= 6;
console.log(y);
// output: 5
In the above example, we equal the variable let y to 30 and then divide with 6 by the assignment operator division after that use echo for printing.
The modulus operator is used for getting the reminder. It is used for integer and float data types.
The variable let y is 35 and we divide it by 4 After dividing it will give us a reminder then we put the variable in console.log for printing. It will return us 3.
let y = 35;
y %= 4;
console.log(y);
// output: 3
In the above example, we equal the variable let y to 35 and then divide with 4 and print the reminder by the assignment operator modulus after that use console.log for printing.
The exponentiation operator is used to exponent the value, it multiplies the base with the exponent which exponent we put. It is used for integer and float data types.
The variable ly is 5 and we input the exponent 3 The operator will multiply the 5 three times then we put the variable in console.log for printing. It will return us 125.
let y = 5;
y **= 3;
console.log(y);
// output: 125
In the above example, We first assign the value 5 to the variable let y. Then, we use the exponentiation assignment operator (**) to raise let y to the power of 3, resulting in let y becoming 125. Finally, we use the “console.log()” command to print this value.
Conclusion:
In this article, we explained assignment operators along with examples. It has also a table in which all assignment operators are listed with their symbols, definitions, and examples.