Loading...

JavaScript Assignment Operators

Assignment Operators in JavaScript

Assignment operators in JavaScript are used to assign values to variables. They not only assign values but can also perform mathematical operations while assigning. This guide will walk you through the various assignment operators, explaining how each one works with clear examples.

= Operator in JavaScript

The = operator assigns the value of the right-hand operand to the left-hand variable.

Example:

javascript
4 lines
|
35/ 500 tokens
1
2
3
4
let x = 20;  // Assigns 20 to x
let y = x + 10;  // Assigns 30 to y (20 + 10)
console.log(x);  // Output: 20
console.log(y);  // Output: 30
Code Tools

+= Operator in JavaScript

The += operator adds the right-hand operand to the left-hand variable and assigns the result to the left-hand variable.

Example:

javascript
3 lines
|
20/ 500 tokens
1
2
3
let a = 15;
a += 10;  // Equivalent to a = a + 10
console.log(a);  // Output: 25
Code Tools

String Example:

javascript
3 lines
|
23/ 500 tokens
1
2
3
let text = "Hello";
text += " JavaScript!";
console.log(text);  // Output: Hello JavaScript!
Code Tools

-= Operator in JavaScript

The -= operator subtracts the right-hand operand from the left-hand variable and assigns the result to the left-hand variable.

Example:

javascript
3 lines
|
20/ 500 tokens
1
2
3
let b = 20;
b -= 5;  // Equivalent to b = b - 5
console.log(b);  // Output: 15
Code Tools

*= Operator in JavaScript

The *= operator multiplies the left-hand variable by the right-hand operand and assigns the result to the left-hand variable.

Example:

javascript
3 lines
|
20/ 500 tokens
1
2
3
let c = 4;
c *= 5;  // Equivalent to c = c * 5
console.log(c);  // Output: 20
Code Tools

**= Operator in JavaScript

The **= operator raises the left-hand variable to the power of the right-hand operand and assigns the result to the left-hand variable.

Example:

javascript
3 lines
|
26/ 500 tokens
1
2
3
let d = 3;
d **= 3;  // Equivalent to d = d ** 3 (3 to the power of 3)
console.log(d);  // Output: 27
Code Tools

/= Operator in JavaScript

The /= operator divides the left-hand variable by the right-hand operand and assigns the result to the left-hand variable.

Example:

javascript
4 lines
|
20/ 500 tokens
1
2
3
let e = 50;
e /= 5;  // Equivalent to e = e / 5
console.log(e);  // Output: 10
Code Tools

%= Operator in JavaScript

The %= operator calculates the remainder of the division of the left-hand variable by the right-hand operand and assigns it to the left-hand variable.

Example:

javascript
3 lines
|
27/ 500 tokens
1
2
3
let f = 29;
f %= 6;  // Equivalent to f = f % 6 (remainder of 29 divided by 6)
console.log(f);  // Output: 5
Code Tools

Frequently Asked Questions

Assignment operators in JavaScript are used to assign values to variables, such as =, +=, -=, *=, and /=.

An assignment operator is used to assign values to variables. For example, let x = 5; assigns the value 5 to the variable x.

In JavaScript, the assign() method is used to copy the values of all enumerable properties from one or more source objects to a target object, often used with objects.

The OR assignment operator (||=) assigns a value to a variable only if the variable is falsy. For example, x ||= 10; will assign 10 to x if x is falsy.

Still have questions?Contact our support team