File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ // Assignment Operators in JavaScript
2+
3+ /*
4+ = Simple assignment
5+ += Add and assign
6+ -= Subtract and assign
7+ *= Multiply and assign
8+ /= Divide and assign
9+ %= Modulus and assign
10+ **= Exponentiation and assign
11+ */
12+
13+ // Simple assignment (=)
14+ // Assigns the value on the right to the variable.
15+ let x = 5 ;
16+ console . log ( x ) ; // Output: 5
17+
18+ // Add and assign (+=)
19+ // Adds right value to left variable and assigns the result.
20+ x += 3 ; // equivalent to x = x + 3
21+ console . log ( x ) ; // Output: 8
22+
23+ // Subtract and assign (-=)
24+ x -= 2 ; // equivalent to x = x - 2
25+ console . log ( x ) ; // Output: 6
26+
27+ // Multiply and assign (*=)
28+ x *= 4 ; // equivalent to x = x * 4
29+ console . log ( x ) ; // Output: 24
30+
31+ // Divide and assign (/=)
32+ x /= 3 ; // equivalent to x = x / 3
33+ console . log ( x ) ; // Output: 8
34+
35+ // Modulus and assign (%=)
36+ x %= 5 ; // equivalent to x = x % 5
37+ console . log ( x ) ; // Output: 3
38+
39+ // Exponentiation and assign (**=)
40+ x **= 3 ; // equivalent to x = x ** 3
41+ console . log ( x ) ; // Output: 27
You can’t perform that action at this time.
0 commit comments