Understanding the Power of Shorthand Assignment Operators in JavaScript


In JavaScript, assignment operators are used to assign values to variables. The basic assignment operator is the "=" sign, which assigns the value on the right side of the operator to the variable on the left side. There are also several shorthand assignment operators that can be used to perform an operation and assignment in one step. These include

Sno Operator Usage
1. = Assigns a value
2. += Adds a value to a variable.
3. -= Subtracts a value from a variable.
4. *= Multiplies a variable.
5. /= Divides a variable.
6. %= Assigns a remainder to a variable.
  • Addition assignment (+=): adds the right-side value to the left-side variable and assigns the result to the left-side variable.
  • Subtraction assignment (-=): subtracts the right-side value from the left-side variable and assigns the result to the left-side variable.
  • Multiplication assignment (*=): multiplies the left-side variable by the right-side value and assigns the result to the left-side variable.
  • Division assignment (/=): divides the left-side variable by the right-side value and assigns the result to the left-side variable.
  • Modulus assignment (%=): finds the remainder when the left-side variable is divided by the right-side value and assigns the result to the left-side variable.
  • Exponentiation assignment (**=): raises the left-side variable to the power of the right-side value and assigns the result to the left-side variable.

It's important to note that these shorthand assignment operators only work with variables, they can not be used with literals. Additionally, the shorthand assignment operators follow the order of precedence of the mathematical operation they are performing.

Another important point is that you should be careful about using the assignment operator within the conditional statements because it can lead to unexpected results if not used correctly.

Source Code

HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tutor Joes</title>
</head>
<body>
 
	<script src="js/script.js"></script>
</body>
</html>
To download raw file Click Here

This code uses the shorthand assignment operators to perform arithmetic operations and assignment in one step.

  • The first line declares a variable a with the value of 10.
  • The next line uses the "+=" shorthand operator to add 5 to the current value of a and assigns the result back to a. This is equivalent to writing a = a + 5; and the value of a will be 15.
  • The next line uses the "-=" shorthand operator to subtract 5 from the current value of a and assigns the result back to a. The value of a will be 10.
  • The next line uses the "*=" shorthand operator to multiply the current value of a by 5 and assigns the result back to a. The value of a will be 50.
  • The next line uses the "/=" shorthand operator to divide the current value of a by 5 and assigns the result back to a. The value of a will be 10.
  • The next line uses the "%=" shorthand operator to finds the remainder when the current value of a is divided by 5 and assigns the result back to a. The value of a will be 0.
  • Finally, the console.log(a) statement is used to display the value of a on the console, which is 0.
script.js
//Assignment Operators
let a=10;
 
//a=a+5; //+=
a+=5;//15
a-=5;//10
a*=5;//50
a/=5;//50
a%=5;//0
console.log(a);
To download raw file Click Here

So the final value of a will be 0, because all the operation are performed on the value of a and the last operation is a%=5 which returns the remainder of a divided by 5 which is 0.

List of Programs


JS Practical Questions & Answers


JS Practical Project