Skip to content

Commit ecf30c8

Browse files
feat: add assignment operators
1 parent 9c600c1 commit ecf30c8

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

part1 (Basics)/16_assignment.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

0 commit comments

Comments
 (0)