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
Copy file name to clipboardExpand all lines: README.md
+92Lines changed: 92 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -203,3 +203,95 @@ Answer - 1undefined
203
203
The if statement is evaluating the function f as a boolean value. In JavaScript, functions are truthy values, so the condition will evaluate to true and the code block inside the if statement will be executed. The value of x is then incremented by the string "function", which is the result of calling typeof f.
204
204
205
205
</details>
206
+
207
+
<details>
208
+
<summary>
209
+
<h3>8. Guess the output of this code?
210
+
211
+
```js
212
+
let a = {
213
+
x:1,
214
+
y:2,
215
+
};
216
+
let b = a;
217
+
a.x=5;
218
+
console.log(a);
219
+
console.log(b);
220
+
```
221
+
222
+
</h3>
223
+
</summary>
224
+
225
+
Answer - {x:5, y:2} {x:5, y:2}
226
+
227
+
JavaScript objects are passed by reference. So when we assigned a object to b. b also pointing to the same object in memory. When we change the value of a.x it also affects b.x
228
+
229
+
</details>
230
+
231
+
<details>
232
+
<summary>
233
+
<h3>9. Guess the output of this code?
234
+
235
+
```js
236
+
let x = [1, 2, 3];
237
+
let y = [1, 2, 3];
238
+
let z = y;
239
+
240
+
console.log(x == y);
241
+
console.log(x === y);
242
+
console.log(z == y);
243
+
console.log(z == x);
244
+
```
245
+
246
+
</h3>
247
+
</summary>
248
+
Answer -
249
+
250
+
false
251
+
false
252
+
true
253
+
false
254
+
255
+
When comparing two objects with the == operator, it compares their references, not their values. In this case, x and y are different objects with the same values. z is assigned the value of y, so they refer to the same object. As a result, the first comparison returns false, the second comparison also returns false and the third comparison returns true. and the last comparison also returns false.
256
+
257
+
</details>
258
+
259
+
<details>
260
+
<summary>
261
+
<h3>10. Guess the output of the below JavaScript code?
262
+
263
+
```js
264
+
var x =0;
265
+
for (let i =0; i <5; i++) {
266
+
setTimeout(() => {
267
+
x++;
268
+
console.log(x);
269
+
}, 1000);
270
+
}
271
+
```
272
+
273
+
</h3>
274
+
</summary>
275
+
276
+
Answer:
277
+
278
+
1
279
+
2
280
+
3
281
+
4
282
+
5
283
+
284
+
The for loop is iterating 5 times, and in each iteration, it is scheduling a function to be invoked after 1000 milliseconds (1 second) using setTimeout.
285
+
This function increments the value of `x` and logs it.
286
+
287
+
But all the 5 functions invoked after 1000 milliseconds.
288
+
289
+
Since, javascript is single threaded and event loop queue all the functions in the event loop and execute them one by one.
290
+
291
+
But inside each `setTimeout` callback execution, `x++` increments `x` value by 1.
292
+
293
+
_It makes difference when position of `x++` code changes wrt the setTimout callback._
294
+
295
+
So all the 5 `callbacks` logs the values in `incremental` way, which is `1 2 3 4 5`.
0 commit comments