-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator.js
More file actions
377 lines (328 loc) · 11 KB
/
operator.js
File metadata and controls
377 lines (328 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//more operator explain
// 1. Arithmetic Operators
// Used for basic mathematical operations.
// + (Addition)
document.write(1+2);
// - (Subtraction)
document.write(1-2);
// * (Multiplication)
document.write(1*2);
// / (Division)
document.write(1/2);
// % (Modulus)
document.write(1%2);
// ++ (Increment)
document.write(a++);
// -- (Decrement)
document.write('Decrement: ');
document.write(b--); // 2 (value of `b` is now 1, but 2 is written before decrement)
document.write('<br>');
document.write('Decrement (after decrement): ');
document.write(b); // 1
document.write('<br>');
// ** (Exponentiation)
document.write('Exponentiation: ');
document.write(c ** 3); // 2 raised to the power of 3 (2^3) = 8
document.write('<br>');
// = (Assignment)
let e = 5;
document.write('Assignment (=): ');
document.write(a); // 5
document.write('<br>');
// += (Addition assignment)
a += 3; // equivalent to a = a + 3;
document.write('Addition assignment (+=): ');
document.write(a); // 8
document.write('<br>');
// -= (Subtraction assignment)
a -= 2; // equivalent to a = a - 2;
document.write('Subtraction assignment (-=): ');
document.write(a); // 6
document.write('<br>');
// *= (Multiplication assignment)
a *= 4; // equivalent to a = a * 4;
document.write('Multiplication assignment (*=): ');
document.write(a); // 24
document.write('<br>');
// /= (Division assignment)
a /= 6; // equivalent to a = a / 6;
document.write('Division assignment (/=): ');
document.write(a); // 4
document.write('<br>');
// %= (Modulus assignment)
a %= 3; // equivalent to a = a % 3;
document.write('Modulus assignment (%=): ');
document.write(a); // 1
document.write('<br>');
// **= (Exponentiation assignment)
a **= 3; // equivalent to a = a ** 3;
document.write('Exponentiation assignment (**=): ');
document.write(a); // 1 (since 1^3 is still 1)
document.write('<br>');
// <<= (Left shift assignment)
let b = 5; // 5 in binary is 101
b <<= 1; // equivalent to b = b << 1;
document.write('Left shift assignment (<<=): ');
document.write(b); // 10 (binary 1010)
document.write('<br>');
// >>= (Right shift assignment)
b >>= 2; // equivalent to b = b >> 2;
document.write('Right shift assignment (>>=): ');
document.write(b); // 2 (binary 10)
document.write('<br>');
// >>>= (Unsigned right shift assignment)
let c = -5; // -5 in binary (32-bit) is 11111111111111111111111111111011
c >>>= 1; // equivalent to c = c >>> 1;
document.write('Unsigned right shift assignment (>>>=): ');
document.write(c); // 2147483645
document.write('<br>');
// &= (Bitwise AND assignment)
let d = 6; // 6 in binary is 110
d &= 3; // equivalent to d = d & 3; (3 in binary is 011)
document.write('Bitwise AND assignment (&=): ');
document.write(d); // 2 (binary 010)
document.write('<br>');
// ^= (Bitwise XOR assignment)
d ^= 1; // equivalent to d = d ^ 1; (1 in binary is 001)
document.write('Bitwise XOR assignment (^=): ');
document.write(d); // 3 (binary 011)
document.write('<br>');
// |= (Bitwise OR assignment)
d |= 2; // equivalent to d = d | 2; (2 in binary is 010)
document.write('Bitwise OR assignment (|=): ');
document.write(d); // 3 (binary 011)
document.write('<br>');
// 3. Comparison Operators
// Used to compare values.
// == (Equal to)
// != (Not equal to)
// === (Strict equal to)
// !== (Strict not equal to)
// > (Greater than)
// < (Less than)
// >= (Greater than or equal to)
// <= (Less than or equal to)
// ? (Ternary/conditional operator)
// == (Equal to)
document.write('Equal to (==): ');
document.write(5 == 5); // true
document.write('<br>');
document.write('Equal to (==): ');
document.write(5 == '5'); // true (type coercion)
document.write('<br>');
// != (Not equal to)
document.write('Not equal to (!=): ');
document.write(5 != 6); // true
document.write('<br>');
document.write('Not equal to (!=): ');
document.write(5 != '5'); // false (type coercion)
document.write('<br>');
// === (Strict equal to)
document.write('Strict equal to (===): ');
document.write(5 === 5); // true
document.write('<br>');
document.write('Strict equal to (===): ');
document.write(5 === '5'); // false (no type coercion)
document.write('<br>');
// !== (Strict not equal to)
document.write('Strict not equal to (!==): ');
document.write(5 !== 6); // true
document.write('<br>');
document.write('Strict not equal to (!==): ');
document.write(5 !== '5'); // true (no type coercion)
document.write('<br>');
// > (Greater than)
document.write('Greater than (>): ');
document.write(6 > 5); // true
document.write('<br>');
// < (Less than)
document.write('Less than (<): ');
document.write(5 < 6); // true
document.write('<br>');
// >= (Greater than or equal to)
document.write('Greater than or equal to (>=): ');
document.write(5 >= 5); // true
document.write('<br>');
// <= (Less than or equal to)
document.write('Less than or equal to (<=): ');
document.write(5 <= 5); // true
document.write('<br>');
// ? (Ternary/conditional operator)
let age = 18;
let canVote = (age >= 18) ? 'Yes, can vote' : 'No, cannot vote';
document.write('Ternary operator (age >= 18 ? "Yes, can vote" : "No, cannot vote"): ');
document.write(canVote); // 'Yes, can vote'
document.write('<br>');
// 4. Logical Operators
// Used for logical operations.
// && (Logical AND)
// || (Logical OR)
// ! (Logical NOT)
// && (Logical AND)
document.write('Logical AND (&&): ');
document.write(true && true); // true
document.write('<br>');
document.write('Logical AND (&&): ');
document.write(true && false); // false
document.write('<br>');
document.write('Logical AND (&&): ');
document.write(false && true); // false
document.write('<br>');
document.write('Logical AND (&&): ');
document.write(false && false); // false
document.write('<br>');
// || (Logical OR)
document.write('Logical OR (||): ');
document.write(true || true); // true
document.write('<br>');
document.write('Logical OR (||): ');
document.write(true || false); // true
document.write('<br>');
document.write('Logical OR (||): ');
document.write(false || true); // true
document.write('<br>');
document.write('Logical OR (||): ');
document.write(false || false); // false
document.write('<br>');
// ! (Logical NOT)
document.write('Logical NOT (!): ');
document.write(!true); // false
document.write('<br>');
document.write('Logical NOT (!): ');
document.write(!false); // true
document.write('<br>');
// Combined logical operations
// let a = true, b = false, c = true;
document.write('Combined logical operations: ');
document.write(a && b || c); // true
document.write('<br>');
document.write('Combined logical operations with NOT: ');
document.write(!a || b && c); // false
document.write('<br>');
// 5. Bitwise Operators
// Operate on binary representations of numbers.
// & (Bitwise AND)
// | (Bitwise OR)
// ^ (Bitwise XOR)
// ~ (Bitwise NOT)
// << (Bitwise left shift)
// >> (Bitwise right shift)
// >>> (Bitwise unsigned right shift)
// & (Bitwise AND)
document.write('Bitwise AND (&): ');
document.write((5 & 3).toString(2)); // 0101 & 0011 = 0001 (binary) = 1 (decimal)
document.write('<br>');
// | (Bitwise OR)
document.write('Bitwise OR (|): ');
document.write((5 | 3).toString(2)); // 0101 | 0011 = 0111 (binary) = 7 (decimal)
document.write('<br>');
// ^ (Bitwise XOR)
document.write('Bitwise XOR (^): ');
document.write((5 ^ 3).toString(2)); // 0101 ^ 0011 = 0110 (binary) = 6 (decimal)
document.write('<br>');
// ~ (Bitwise NOT)
document.write('Bitwise NOT (~): ');
document.write((~5).toString(2)); // ~0101 = 1010 (binary) = -6 (decimal)
document.write('<br>');
// << (Bitwise left shift)
document.write('Bitwise left shift (<<): ');
document.write((5 << 1).toString(2)); // 0101 << 1 = 1010 (binary) = 10 (decimal)
document.write('<br>');
// >> (Bitwise right shift)
document.write('Bitwise right shift (>>): ');
document.write((5 >> 1).toString(2)); // 0101 >> 1 = 0010 (binary) = 2 (decimal)
document.write('<br>');
// >>> (Bitwise unsigned right shift)
document.write('Bitwise unsigned right shift (>>>): ');
document.write((-5 >>> 1).toString(2)); // -5 in 32-bit binary is 11111111111111111111111111111011
// >>> 1 = 01111111111111111111111111111101 (binary) = 2147483645 (decimal)
document.write('<br>');
// 6. String Operators
// Primarily used for string concatenation.
// + (Concatenation)
// += (Concatenation assignment)
// + (Concatenation)
let str1 = 'Hello';
let str2 = 'World';
document.write('Concatenation (+): ');
document.write(str1 + ' ' + str2); // Hello World
document.write('<br>');
// += (Concatenation assignment)
let greeting = 'Hi';
let name = 'Alice';
greeting += ', ' + name + '!';
document.write('Concatenation assignment (+=): ');
document.write(greeting); // Hi, Alice!
document.write('<br>');
// 7. Conditional (Ternary) Operator
// Shorthand for an if-else statement.
// condition ? expr1 : expr2
// Basic example
let message = (age >= 18) ? 'Adult' : 'Minor';
document.write('Basic example: ');
document.write(message); // Adult
document.write('<br>');
// Using expressions
let num1 = 5, num2 = 10;
let max = (num1 > num2) ? num1 : num2;
document.write('Using expressions: ');
document.write(max); // 10
document.write('<br>');
// Nested ternary operator
let score = 85;
let grade = (score >= 90) ? 'A' : (score >= 80) ? 'B' : (score >= 70) ? 'C' : 'F';
document.write('Nested ternary operator: ');
document.write(grade); // B
document.write('<br>');
// Ternary operator with function call
function isEven(num) {
return (num % 2 === 0) ? 'Even' : 'Odd';
}
document.write('Ternary operator with function call: ');
document.write(isEven(7)); // Odd
document.write('<br>');
// 8. Comma Operator
// Evaluates multiple expressions and returns the last expression.
// , (Comma)
// Example 1: Basic usage
let a = (5 + 2, 7 * 2);
document.write('Example 1 (Basic usage): ');
document.write(a); // 14
document.write('<br>');
// Example 2: Function argument
function increment(x) {
return (x++, x);
}
let F = increment(5);
document.write('Example 2 (Function argument): ');
document.write(b); // 6
document.write('<br>');
// Example 3: Multiple assignments
c = 5, d = 10;
document.write('Example 3 (Multiple assignments): ');
document.write(c + d); // 15
document.write('<br>');
// Example 4: Increment multiple variables
num1++, num2++;
document.write('Example 4 (Increment multiple variables): ');
document.write(num1 + ', ' + num2); // 2, 3
document.write('<br>');
// 9. Unary Operators
// Operate on a single operand.
// + (Unary plus)
// - (Unary negation)
// ! (Logical NOT)
// ~ (Bitwise NOT)
// typeof (Type of operator)
// void (Discards a return value)
// delete (Deletes an object property)
// 10. Relational Operators
// Used to test relationships between two variables or expressions.
// in (Property in object)
// instanceof (Instance of object)
// 11. Nullish Coalescing Operator
// Provides a default value if the left-hand operand is null or undefined.
// ?? (Nullish coalescing)
// 12. Optional Chaining Operator
// Allows safe access to nested object properties.
// ?. (Optional chaining)