-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavascriptOperators.js
224 lines (176 loc) · 4.71 KB
/
JavascriptOperators.js
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
let x = 1;
x++; // Pre or post-increment - in this case, pre-increment
x--; // Pre or post-decrement, here, post decrement
x = x-1; //negate number
x = x+1; // add to number
x = 5;
invertedX = ~x;
// The binary
// representation of 5
// is 00000000000000000000000000000101
// applying ~ operator flips each bit
// result: 11111111111111111111111111111010
xBool = true
xBool = !xBool
// ! inverts boolean
let obj = {age:25}
delete obj.age;
// obj now empty
// delete operator: deletes
typeOf(xBool); // Boolean
x = 5;
result = void(x); // result: undefined
let xPonentiate = x ** x; // x to the xthpower
x = x*x; //multiply
x = x/x; //divide
x = x%x; //remainder/moddulo
x = x + x; // add
x = x-x; //subtract
string1 = "Hello"
string2 = "world"
string3 = string1 + string2 //"Hello world"
//shift left:
x = 5;
// Binary: 00000000000000000000000000000101
shiftLeft = x << 2;
// shifts binary x left by two positions
console.log(shiftLeft) // result 20,
// binary 00000000000000000000000000010100
//shift right with sign extension:
x = -20;
// Binary 11111111111111111111111111101100
let result = x >> 2; // Shift right by 2 positions
console.log(result); // Output: -5
// Binary 11111111111111111111111111111011
// >>>: shifts bits of its first operand to
// right by number of positions specified
// by second operand
// bits shifted off to the right are discarded
// zeros are shifted in from the left
// .
x = -20;
//Binary 11111111111111111111111111101100
x = x >>> 2;
// Shift right by 2 positions with zero extension
console.log(x) // Output: 1073741819
//binary: 00111111111111111111111111111011
x = 5;
y = 10;
x > y; // Compare: False
x < y; // Compare: True
x <= y; // Compare: True
x >= y; // Compare: false
aString = "Amazon"
bString = "Blueberry"
aString > bString // True: Compare in alphabetical order
// using instanceOf operator
class Fruit{}
class Apple extends Fruit{}
let apple = new Apple();
console.log (apple instanceof Apple); // true
console.log(apple instanceof Fruit); // true
console.log(apple instanceof Object); //true
console.log(apple instanceof Array); //false
let obj2 = {
name: "Alice",
age: 25
};
console.log("name" in obj); //true
console.log("age" in obj) // True
console.log("gender" in obj) // false
console.log("toString" in obj)
// true, checks if toString
// property exists in obj,
// returns true as toString
// is inherited from Object.prototype
// Non-Strict vs Strict Equality
console.log(5 == "5");
// true, number and string
// are converted to same type
console.log(null == undefined);
// true (null and undefined
// are considered equal)
console.log(0 == false);
// true (0 is converted to
// false)
//Strict Equality
console.log(5 === "5");
//false, not same type
console.log(null == undefined);
// false, different types
console.log(0 == false);
// false, different types
// &: bitwise AND operator
a = 5;
b = 3;
result = a & b;
console.log(result)
//output: 1
// each bit in the result is set to 1
// if bits of both a and b are 1
//otherwise it is set to 0
// bitwise XOR exclusive OR
result = a ^ b;
// each bit in result is set to 1
// if corresponding bits of a and b
// are different, otherwise set to 0
result = a | b;
//bitwise OR
// each bit in result is set to 1
// if at least one of corresponding bits
// is 1, otherwise is 0
// &&: logical AND operator
// returns true if both a and b are
// true, otherwise false
// stops evaluating upon encountering
// "false"
let a = true;
let b = false;
console.log (a && b) //false
console.log(a && true); //true
console.log(b && true); //false
// || compute logical OR
// returns true if at least one
// expression is true.
console.log( a || b); // true
console.log(a || false); // true
console.log (b || false); //false
// ?? operator
// returns first operand if
// not null or undefined
//otherwise, returns second operand
// useful for providing default values
let z = null;
let q = "default"
let check = z ?? q;
// Choose first defined operand
console.log(check)
//Output: default
z = "defined value"
console.log(check)
//output: "defined value"
// ?: ternary operator
// choose between 2 values
// based on condition
//Evaluates condiiton and returns
// one of two values based on if
//condition is true or false
x = 10;
y = 20;
let comparison = x > y ? "x greater" : "y greater/equal";
console.log(comparison)
//output: y greater or equal
x = x+1; // = assignment operator
x **= x+1; // Operate and assign
x *= x+1; // operate and assign
x /= x +1; // operate and assign
x %= x+1; //operate and assign
// also +=, -=, &=, ^=, |=,
// <<=, >>=, >>>=.
// ',' comma operator
a =1;
b=2;
result = (a++, b++);
console.log(result) // Output: 2
// b does not increment until after return
// comma skips over a, returns value of last expression