Assignment Operators in javascript

Assignment Operators in javascript

In JavaScript programming, the assignment operator is used to assign a value to a user-declared variable. In any JavaScript program, the assignment operator is used to declare and assign or update a value in a user-declared variable. The most useful assignment operator is the equal operator =. Here you get many types of assignment operator options in JavaScript. Using the assignment operator, you can perform many mathematical operations, such as adding, subtracting, multiplying, division, modulus, and exponential values, etc.

Assignment Operators in javascript

Basic assignment operator (=) operator.

Assignment Operators in javascript.

The most basic assignment operator is equal =, which is used to assign a value to a user-declared variable.

Basic assignment operator (=) operator syntax.

let variable = value;

Example of assignment operator (=) operator.

let p = 22; // Here the variable p is assigned the value 22.

let q = 31; // Here variable q is assigned the value 31

Addition assignment (+=) operator.

Here the add assignment += operator in the current JavaScript program previews adding the right operand to the left operand, and assigns the result to the left operand.

Addition assignment (+=) operator syntax.

declare_variable += value;

Example of Addition assignment (+=) operator.

let p = 4;

p += 10; // p = p + 10 (4 + 10)

console.log(p); // Result – 14

Subtraction assignment (-=) operator.

The subtraction assignment -= operator in the JavaScript program subtracts the left operand from the right operand, and assigns the result to the left operand.

Subtraction assignment (-=) operator syntax.

declare_variable -= value;

Example of subtraction assignment (-=) operator.

let p = 8;

p -= 2; // p = p – 2 (8 – 2)

console.log(p); // Result – 6

Multiplication assignment (*=) operator.

Multiplication in JavaScript program *= operator multiplies the left operand by the right operand, and assigns the output value of the multiplication result of the variable value to the left operand.

Syntax of the multiplication assignment (*=) operator.

decalre_variable *= value;

Example of the multiplication assignment (*=) operator.

let p = 4;

p *= 3; // p = p * 3 (4 * 3)

console.log(p); // result – 12

Division assignment (/=) operator.

The division assignment /= operator divides the left operand by the right operand, and assigns the result to the left operand.

Syntax of the division assignment (/=) operator.

declare_variable /= value;

Example of the division assignment (/=) operator.

let p = 25;

p /= 5; // p = p / 5 (25 / 5)

console.log(p); // result – 5

Remainder (modulo) assignment (%=) operator.

The %= operator in JavaScript programs divides the left operand by the right operand, and assigns the modulus of the division of the two to the left operand.

The syntax of the remainder (modulo) assignment (%=) operator.

declare_variable %= value;

Example of the remainder (modulo) assignment (%=) operator.

let p = 11;

p %= 3; // p = p % 3 (11 % 3)

console.log(p); // result – 2 (remainder of 11 divided by 3 is 2)

Exponentiation (**=) operator.

The exponent **= operator in JavaScript programs raises the left operand to the exponent value of the right operand, and assigns its result to the value of the left operand.

Syntax of exponentiation (**=) operator.

declare_variable **= value;

Example of exponentiation (**=) operator.

let p = 2;

p **= 4; // p = p ** 3 (2 ** 4)

console.log(p); // result – 16 (2 raised to the power of 4 and result is 16)

JavaScript assignment operator Summary.

OperatorDescriptionExampleResult
=Equal Assigns operator used to a value to a variablep = 1;p is 1
+=Add assignment operator used to Adds the right operand to the left operandp += 3;p is p + 3
-=Subtract assignment operator used to Subtracts the right operand from the left operandp -= 4;p is p – 4
*=Multiply assignment operator used to Multiplies the left operand by the right operandp *= 5;p is p * 5
/=Division assignment operator used to Divides the left operand by the right operandp /= 3;p is p / 3
%=Remainder assignment operator used to Assigns the remainder of the divisionp %= 2;p is p % 2
**=Exponent assignment operator used to Raises the left operand to the power of the right operandp **= 3;p is p **

So let’s see the example of using multiple assignment operator in JavaScript programming.

Example of combination of JavaScript multiple assignment operator.

let p = 7;

p += 2; // p = 7 + 2 -> p = 9

p -= 3; // p = 7 – 3 -> p = 4

p *= 2; // p = 7 * 2 -> p = 14

p /= 4; // p = 2 / 4 -> p = 1.75

p %= 5; // p = 7 % 5 -> p = 2

p **= 2; // p = 1 ** 2 -> p = 49

console.log(p); // result – 9

JavaScript Assignment Operator Conclusion.

Assignment operators in JavaScript programming make it easy to update or modify the value of a variable with multiple program operations. Instead of creating a detailed expression like where p = p + 7, JavaScript programmers can simply use the arithmetic assignment += operator to achieve the equivalent result (p += 7).