How to use the (=) operator in JavaScript
The assignment operator = in JavaScript is used to assign a value to a variable. The basic syntax for using the assignment operator is as follows:
variable_name = value;
For example:
let x = 5;
let name = "John";
You can also use the assignment operator to reassign a new value to a variable.
For example:
let x = 5;
x = 10;
Here, the value of x is first set to 5, and then it's reassigned to 10.
You can also use the assignment operator to chain multiple assignment together:
let x = y = z = 0;
Here the value of x, y, and z all get assigned to 0.
Also, you can use the operator in mathematical expression and the result will be assigned to the variable
let x = 5;
x += 3;
console.log(x); // 8
This is equivalent to writing x = x + 3;.
It is important to note that when using the assignment operator =, the value is assigned to the variable on the left side of the operator, and the value on the right side of the operator is the value being assigned to the variable.
The assignment operator = can also be used in combination with other operators like the arithmetic operator, the logical operator, and the comparison operator.
let x = 5, y = 3;
x += y;
console.log(x); // 8
Here the value of y is added to the value of x and the result is assigned to x. This is equivalent to writing x = x + y;.
It is important to note that when using the assignment operator in combination with other operators, the operation on the right side of the operator is performed first and then the result is assigned to the variable on the left side of the operator.
How to use the (+=) operator in JavaScript
The addition assignment operator += in JavaScript is a shorthand way of adding a value to a variable and reassigning the result to the same variable. The basic syntax for using the addition assignment operator is as follows:
variable_name += value;
For example:
let x = 5;
x += 3;
console.log(x); // 8
This is equivalent to writing x = x + 3;.
You can also use the addition assignment operator to add a value to a variable and reassign the result to the same variable:
let x = 5;
x += 3;
console.log(x); // 8
It is also possible to use the addition assignment operator to concatenate strings, where the operator combines two or more strings together:
let firstName = "John";
firstName += " Doe";
console.log(firstName); // "John Doe"
The addition assignment operator += can also be used in combination with other arithmetic operators such as -=, *=, /=, %=.
let x = 5, y = 3;
x *= y;
console.log(x); // 15
Here the value of y is multiplied with the value of x and the result is assigned to x. This is equivalent to writing x = x * y;.
It is important to note that when using the addition assignment operator in combination with other operators, the operation on the right side of the operator is performed first and then the result is assigned to the variable on the left side of the operator.
Also, the addition assignment operator += expects two operands that are numbers, if not it will try to convert them to numbers before performing the operation.
How to use the (-=) operator in JavaScript
The subtraction assignment operator -= in JavaScript is a shorthand way of subtracting a value from a variable and reassigning the result to the same variable. The basic syntax for using the subtraction assignment operator is as follows:
variable_name -= value;
For example:
let x = 5;
x -= 3;
console.log(x); // 2
This is equivalent to writing x = x - 3;.
You can also use the subtraction assignment operator to subtract a value from a variable and reassign the result to the same variable:
let x = 5;
x -= 3;
console.log(x); // 2
The subtraction assignment operator -= can also be used in combination with other arithmetic operators such as +=, *=, /=, %=.
let x = 5, y = 3;
x *= y;
console.log(x); // 15
Here the value of y is multiplied with the value of x and the result is assigned to x. This is equivalent to writing x = x * y;.
It is important to note that when using the subtraction assignment operator in combination with other operators, the operation on the right side of the operator is performed first and then the result is assigned to the variable on the left side of the operator.
Also, the subtraction assignment operator -= expects two operands that are numbers, if not it will try to convert them to numbers before performing the operation.
How to use the (*=) operator in JavaScript
The multiplication assignment operator *= in JavaScript is a shorthand way of multiplying a variable by a value and reassigning the result to the same variable. The basic syntax for using the multiplication assignment operator is as follows:
variable_name *= value;
For example:
let x = 5;
x *= 3;
console.log(x); // 15
This is equivalent to writing x = x * 3;.
You can also use the multiplication assignment operator to multiply a variable by a value and reassign the result to the same variable:
let x = 5;
x *= 3;
console.log(x); // 15
The multiplication assignment operator *= can also be used in combination with other arithmetic operators such as +=, -=, /=, %=.
let x = 5, y = 3;
x *= y;
console.log(x); // 15
Here the value of y is multiplied with the value of x and the result is assigned to x. This is equivalent to writing x = x * y;.
It is important to note that when using the multiplication assignment operator in combination with other operators, the operation on the right side of the operator is performed first and then the result is assigned to the variable on the left side of the operator.
Also, the multiplication assignment operator *= expects two operands that are numbers, if not it will try to convert them to numbers before performing the operation.
How to use the (/=) operator in JavaScript
The division assignment operator /= in JavaScript is a shorthand way of dividing a variable by a value and reassigning the result to the same variable. The basic syntax for using the division assignment operator is as follows:
variable_name /= value;
For example:
let x = 15;
x /= 3;
console.log(x); // 5
This is equivalent to writing x = x / 3;.
You can also use the division assignment operator to divide a variable by a value and reassign the result to the same variable:
let x = 15;
x /= 3;
console.log(x); // 5
The division assignment operator /= can also be used in combination with other arithmetic operators such as +=, -=, *=, %=.
let x = 15, y = 3;
x /= y;
console.log(x); // 5
Here the value of x is divided by the value of y and the result is assigned to x. This is equivalent to writing x = x / y;.
It is important to note that when using the division assignment operator in combination with other operators, the operation on the right side of the operator is performed first and then the result is assigned to the variable on the left side of the operator.
Also, the division assignment operator /= expects two operands that are numbers, if not it will try to convert them to numbers before performing the operation.
Also, when dividing by zero, JavaScript returns infinity or -infinity, and when dividing a non-finite number by infinity, JavaScript returns zero.
How to use the (%=) operator in JavaScript
The modulus assignment operator %= in JavaScript is a shorthand way of finding the remainder of a variable divided by a value and reassigning the result to the same variable.
The basic syntax for using the modulus assignment operator is as follows:
variable_name %= value;
For example:
let x = 15;
x %= 3;
console.log(x); // 0
This is equivalent to writing x = x % 3;.
You can also use the modulus assignment operator to find the remainder of a variable divided by a value and reassign the result to the same variable:
let x = 15;
x %= 3;
console.log(x); // 0
The modulus assignment operator %= can also be used in combination with other arithmetic operators such as +=, -=, *=, /=.
let x = 15, y = 3;
x %= y;
console.log(x); // 0
Here the value of x is divided by the value of y and the remainder is assigned to x. This is equivalent to writing x = x % y;.
It is important to note that when using the modulus assignment operator in combination with other operators, the operation on the right side of the operator is performed first and then the result is assigned to the variable on the left side of the operator.
Also, the modulus assignment operator %= expects two operands that are numbers, if not it will try to convert them to numbers before performing the operation. Also, when the divisor is zero, JavaScript returns NaN.
How to use the (**=) operator in JavaScript
The exponentiation assignment operator **= in JavaScript is a shorthand way of raising a variable to a power and reassigning the result to the same variable. The basic syntax for using the exponentiation assignment operator is as follows:
variable_name **= value;
For example:
let x = 2;
x **= 3;
console.log(x); // 8
This is equivalent to writing x = x ** 3;.
You can also use the exponentiation assignment operator to raise a variable to a power and reassign the result to the same variable:
let x = 2;
x **= 3;
console.log(x); // 8
The exponentiation assignment operator **= can also be used in combination with other arithmetic operators such as +=, -=, *=, /=, %=.
let x = 2, y = 3;
x **= y;
console.log(x); // 8
Here the value of x is raised to the power of y and the result is assigned to x. This is equivalent to writing x = x ** y;.
It is important to note that when using the exponentiation assignment operator in combination with other operators, the operation on the right side of the operator is performed first and then the result is assigned to the variable on the left side of the operator.
Also, the exponentiation assignment operator **= expects two operands that are numbers, if not it will try to convert them to numbers before performing the operation.
Please note that this operator is not supported by older version of Javascript. You will have to use Math.pow() for older versions.