Skip to content

Commit 62c03ee

Browse files
committed
operator
1 parent 2a1b2af commit 62c03ee

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

Variables/operator.js

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
2+
3+
// 1. Arithmetic Operators
4+
let a = 10, b = 3;
5+
6+
console.log(a + b); // 13 (Addition)
7+
console.log(a - b); // 7 (Subtraction)
8+
console.log(a * b); // 30 (Multiplication)
9+
console.log(a / b); // 3.333... (Division)
10+
console.log(a % b); // 1 (Modulus / Remainder)
11+
console.log(a ** b); // 1000 (Exponentiation a^b)
12+
13+
//Increment & Decrement:
14+
15+
a++; // Post-increment
16+
++a; // Pre-increment
17+
a--; // Post-decrement
18+
--a; // Pre-decrement
19+
20+
// 2. Assignment Operators
21+
22+
//Assign values to variables.
23+
let x = 10;
24+
25+
x += 5; // x = 15
26+
x -= 3; // x = 12
27+
x *= 2; // x = 24
28+
x /= 4; // x = 6
29+
x %= 5; // x = 1
30+
x **= 3; // x = 1^3 = 1
31+
32+
33+
34+
//🔹 3. Logical Operators
35+
36+
//Used for boolean logic.
37+
38+
console.log(true && false); // false (AND)
39+
console.log(true || false); // true (OR)
40+
console.log(!true); // false (NOT)
41+
42+
43+
//4. Ternary Operator
44+
45+
//Short-hand for if...else.
46+
let age = 18;
47+
let result = (age >= 18) ? "Adult" : "Minor";
48+
console.log(result); // "Adult"
49+
50+
51+
//5. Comparison Operators
52+
53+
console.log(5 == "5"); // true (loose equality, type conversion allowed)
54+
console.log(5 === "5"); // false (strict equality, no type conversion)
55+
console.log(5 != "5"); // false loose checking
56+
console.log(5 !== "5"); // true strict checking
57+
console.log(5 > 3); // true
58+
console.log(5 < 3); // false
59+
console.log(5 >= 5); // true
60+
console.log(5 <= 3); // false
61+
62+
/*
63+
== vs === → loose vs strict equality.
64+
65+
!= vs !== → loose vs strict inequality.
66+
67+
> , < , >= , <= → normal number comparison.
68+
*/
69+
70+
71+
72+
// 5. Bitwise Operators
73+
74+
//Operate at the binary level.
75+
76+
console.log(5 & 1); // 1 (AND)
77+
console.log(5 | 1); // 5 (OR)
78+
console.log(5 ^ 1); // 4 (XOR)
79+
console.log(~5); // -6 (NOT)
80+
console.log(5 << 1); // 10 (Left shift)
81+
console.log(5 >> 1); // 2 (Right shift)
82+
83+
84+
85+
/*
86+
87+
1. console.log(5 & 1); // 1 (AND)
88+
89+
👉 Bitwise AND (&) → 1 if both bits are 1.
90+
91+
0101 (5)
92+
& 0001 (1)
93+
----
94+
0001 (1 in decimal)
95+
96+
97+
✅ Result = 1
98+
99+
2. console.log(5 | 1); // 5 (OR)
100+
101+
👉 Bitwise OR (|) → 1 if either bit is 1.
102+
103+
0101 (5)
104+
| 0001 (1)
105+
----
106+
0101 (5 in decimal)
107+
108+
109+
✅ Result = 5
110+
111+
3. console.log(5 ^ 1); // 4 (XOR)
112+
113+
👉 Bitwise XOR (^) → 1 if bits are different.
114+
115+
0101 (5)
116+
^ 0001 (1)
117+
----
118+
0100 (4 in decimal)
119+
120+
121+
✅ Result = 4
122+
123+
4. console.log(~5); // -6 (NOT)
124+
125+
👉 Bitwise NOT (~) → Flips every bit (0 → 1, 1 → 0).
126+
JavaScript uses 32-bit signed integers internally for bitwise ops.
127+
128+
Binary of 5 (32-bit):
129+
130+
00000000 00000000 00000000 00000101 (5)
131+
132+
133+
Flipping bits:
134+
135+
11111111 11111111 11111111 11111010
136+
137+
138+
This is two’s complement representation → equals -6.
139+
✅ Result = -6
140+
141+
5. console.log(5 << 1); // 10 (Left shift)
142+
143+
👉 Left shift (<<) → shifts bits left, filling with 0s.
144+
145+
0101 (5)
146+
<< 1
147+
----
148+
1010 (10 in decimal)
149+
150+
151+
✅ Result = 10
152+
153+
6. console.log(5 >> 1); // 2 (Right shift)
154+
155+
👉 Right shift (>>) → shifts bits right, preserving sign.
156+
157+
0101 (5)
158+
>> 1
159+
----
160+
0010 (2 in decimal)
161+
162+
163+
✅ Result = 2
164+
*/

0 commit comments

Comments
 (0)