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. |
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.
<!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.
//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.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions