You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An expression is a valid piece of code that resolves to a value. There are two types of expressions: those that have effects such as asssigning the value and those who evaluate value.
@@ -6,8 +21,13 @@ For example: `x = 7` is a expression that assigns the value of 7 to the variable
6
21
7
22
## Types of Operator
8
23
9
-
- Assignment Operator: It assigns a value to its left operand to the value at its right operand with the operator `=`.
10
-
- Comparision Operator: It compares its operands and returns a logical value based on whether the comparision is `true` or `false`. If two operands are not of same type, JS attempts to convert them to appropriate type for comparision.
24
+
### `Assignment Operator`
25
+
26
+
It assigns a value to its left operand to the value at its right operand with the operator `=`.
27
+
28
+
### `Comparision Operator`
29
+
30
+
It compares its operands and returns a logical value based on whether the comparision is `true` or `false`. If two operands are not of same type, JS attempts to convert them to appropriate type for comparision.
@@ -20,7 +40,9 @@ For example: `x = 7` is a expression that assigns the value of 7 to the variable
20
40
| Less than (<) | Returns true if the left operand is less than the right operand. |
21
41
| Less than or equal (<=) | Returns true if the left operand is less than or equal to the right operand. |
22
42
23
-
- Arithematic Operator: It takes numerical values as operands and returns single numerical value.The standard arithematic operations are addition `(+)`, subtraction `(-)`, multiplication `(*)` and division `(/)`.
43
+
### `Arithematic Operator`
44
+
45
+
It takes numerical values as operands and returns single numerical value.The standard arithematic operations are addition `(+)`, subtraction `(-)`, multiplication `(*)` and division `(/)`.
24
46
25
47
```js
26
48
20/2; //10
@@ -41,7 +63,9 @@ In addition to the standard arithematic operations `(+,-,*,/)`, JS also provides
41
63
| Less than (<) | Returns true if the left operand is less than the right operand. |
42
64
| Less than or equal (<=) | Returns true if the left operand is less than or equal to the right operand. |
43
65
44
-
- Bitwise Operator: Bitwise operator treats their operands as a set of 32 bits (1s and 0s). For example, the decimal `7` has a binary represntation of `111`. Bitwise operators perform their operations on such binary representations but they return numerical values.
66
+
###` Bitwise Operator`
67
+
68
+
Bitwise operator treats their operands as a set of 32 bits (1s and 0s). For example, the decimal `7` has a binary represntation of `111`. Bitwise operators perform their operations on such binary representations but they return numerical values.
@@ -50,4 +74,171 @@ In addition to the standard arithematic operations `(+,-,*,/)`, JS also provides
50
74
| Bitwise XOR (`a ^ b`) | Returns a zero in each bit position for which the corresponding bits are the same. |
51
75
| Bitwise NOT (`~ a`) | Inverts the bits of its operand. |
52
76
53
-
- Logical Operator: They are typically used with boolean values however `&&` and `||` operators actually return the value of one of the specified operands,so if the operands are used with non-boolean values, they may return non-boolean values.
77
+
### `Logical Operator`
78
+
79
+
They are typically used with boolean values however `&&` and `||` operators actually return the value of one of the specified operands,so if the operands are used with non-boolean values, they may return non-boolean values.
| Logical AND `(&&)`|`(a && b)` returns `a` if it can be converted to false; otherwise, it returns `b`. Thus, when used with Boolean values, `&&` returns `true` if both operands are `true`; otherwise, returns `false`. |
84
+
| Logical OR `(\|\|)`|`(a \|\| b)` returns `a` if it can be converted to true; otherwise, it returns `b`. Thus, when used with Boolean values, `\|\|` returns `true` if either operand is `true`; if both are `false`, returns `false`. |
85
+
| Logical NOT `(!)`|`(!a)` returns `false` if its single operand can be converted to `true`; otherwise, it returns `true`. |
86
+
87
+
#### `Logical AND (&&)`
88
+
89
+
```js
90
+
constweirdo="Cat"&&"Dog"; //OUTPUT: Dog
91
+
```
92
+
93
+
Here, `Cat && Dog` evaluates to "Dog" because as mentioned above, `&&` operator when used with two operands, evaluates left operand first and if it can be converted to `false`, it returns the left-hand operand _(Cat in this case)_ else returns the right-hand operand _(Dog in this case)_.
94
+
95
+
To elaborate, the left-hand operand "Cat" can be converted to `true`, since it is not a empty string. Therefore, JavaScript proceeds to evaluate right-hand operand "Dog" which also can be converted to `true`. _Since, both the operands are `true`, logical AND operator returns the right-hand operator_ i.e. Dog.
96
+
97
+
**More Examples of `&&` Operator:**
98
+
99
+
```js
100
+
consta1=true&&true; // returns true
101
+
consta2=true&&false; // returns false
102
+
consta3=false&&true; // returns false
103
+
consta4=false&&3===4; // returns false
104
+
consta5="Cat"&&"Dog"; // returns Dog
105
+
consta6=false&&"Cat"; // returns false
106
+
consta7="Cat"&&false; // returns false
107
+
```
108
+
109
+
You should try to understand these expressions and the reasons for yourself.
110
+
111
+
#### `Logical OR (||)`
112
+
113
+
```js
114
+
constnextWeirdo="Cat"||"Dog"; //OUPUT: Cat
115
+
```
116
+
117
+
Here, `Cat || Dog` evaluates to "Cat" because as mentioned above, `||` operator when used with two operands, evaluates left operand first and if it can be converted to `true`, it returns the left-hand operand _(Cat in this case)_ else returns the right-hand operand _(Dog in this case)_.
118
+
119
+
To elaborate, the left-hand operand "Cat" can be converted to `true`, since it is not a empty string. Therefore, JavaScript returns the left-hand operand i.e. "Cat". The right-hand operand "Dog" is not even evaluated since the right-hand operand is already `true`.
120
+
121
+
**More Examples of `||` Operator:**
122
+
123
+
```js
124
+
consto1=true||true; // returns true
125
+
consto2=false||true; // returns true
126
+
consto3=true||false; // returns true
127
+
consto4=false||3===4; // returns false
128
+
consto5="Cat"||"Dog"; // returns Cat
129
+
consto6=false||"Cat"; // returns Cat
130
+
consto7="Cat"||false; // returns Cat
131
+
```
132
+
133
+
#### `Logical NOT (!)`
134
+
135
+
```js
136
+
constn1=!true; // returns false
137
+
constn2=!false; // returns true
138
+
constn3=!"Cat"; // returns false
139
+
```
140
+
141
+
### `Conditional (Ternary) Operator`
142
+
143
+
Conditional Operator is the only JavaScript operator that takes three operands.
144
+
145
+
Syntax:
146
+
147
+
```
148
+
condition ? val1 : val2;
149
+
```
150
+
151
+
If the condition is `true`, the operator has the value of `val1` else it has the value of `val2`. We can use the ternary operator anywhere we would use standard operator.
152
+
153
+
Example:
154
+
155
+
```js
156
+
constentry= age >=18?"accepted":"rejected";
157
+
```
158
+
159
+
This statement assigns the value of `entry` to "accepted" if the age is 18 or more else it assigns the value to "rejected".
160
+
161
+
### `Unary Operator`
162
+
163
+
A unary operation is an operation with only one operand.
164
+
165
+
-`delete`: it deletes and object's property.
166
+
167
+
```
168
+
delete object.property;
169
+
delete object[propertyKey];
170
+
delete objectName[index];
171
+
```
172
+
173
+
If the `delete` operator succeeds, it removes the property from the object. When we try to access that property afterwards, it will return `undefined`.
Well, you may be wondering why `0 in fruits` evaluates to `true` but `apple in fruits` evaluate to `false`.
236
+
```
237
+
238
+
The reason for this is because the `in` operator in JavaScript is used to check if a specified property/key exists in an `object` or `array`.
239
+
240
+
In the case of `0 in fruits`, it is checking whether the fruits array has a property/key of 0, which it does since arrays in JavaScript are objects with numeric keys starting from 0. Therefore, the expression 0 in fruits returns `true`.
241
+
242
+
However, "apple" in fruits is checking whether the fruits array has a property/key of "apple", which it does not, since the elements of the array are not considered keys. Therefore, the expression "apple" in fruits returns `false`.
243
+
244
+
To check if a value exists in an array, you can use the `includes()` method instead: `fruits.includes("apple")` would return `true` because the value "apple" exists in the array.
0 commit comments