JavaScript Assignment

In JavaScript, assignment operators are used to assign values to variables. They combine the assignment operation with other arithmetic or logical operations. Here are the commonly used assignment operators:


1. Assignment (=):

- Assigns a value to a variable.
- Example: `let x = 5;`


2. Addition assignment (+=):

- Adds a value to the variable and assigns the result to the variable.
- Example: `let x = 10; x += 5; // x = 15`


3. Subtraction assignment (-=):

- Subtracts a value from the variable and assigns the result to the variable.
- Example: `let x = 10; x -= 3; // x = 7`


4. Multiplication assignment (*=):

- Multiplies the variable by a value and assigns the result to the variable.
- Example: `let x = 5; x *= 3; // x = 15`


5. Division assignment (/=):

- Divides the variable by a value and assigns the result to the variable.
- Example: `let x = 20; x /= 4; // x = 5`


6. Remainder/Modulus assignment (%=):

- Calculates the remainder of the division between the variable and a value, and assigns the result to the variable.
- Example: `let x = 15; x %= 4; // x = 3`


7. Exponentiation assignment (**=):

- Raises the variable to the power of a value and assigns the result to the variable.
- Example: `let x = 2; x **= 3; // x = 8`


Assignment operators provide a convenient way to perform an operation on a variable and update its value in a single step. They are commonly used to increment or decrement variables, accumulate values, or update variables based on calculations.

It's worth noting that assignment operators can be combined with other operators, such as addition (+), subtraction (-), multiplication (*), and division (/), to perform compound assignments. For example, `x += 5` is equivalent to `x = x + 5`.

Using assignment operators can make code concise and improve readability by reducing the number of lines required to perform operations and update variables.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext