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
<spanstyle="font-size: 0.8rem; border-bottom: 1pxsolidgrey;"> Updated Jan 11, 2023 </span>
7
+
<spanstyle="font-size: 0.8rem; border-bottom: 1pxsolidgrey;"> Updated Jan 19, 2023 </span>
8
8
9
9
In this article, we will cover a range of JavaScript interview questions, including those related to the latest versions of the language (ES6, ES7, ES8, and ES9).
10
10
@@ -295,3 +295,98 @@ _It makes difference when position of `x++` code changes wrt the setTimout callb
295
295
So all the 5 `callbacks` logs the values in `incremental` way, which is `1 2 3 4 5`.
296
296
297
297
</details>
298
+
299
+
<details>
300
+
<summary>
301
+
<h3>11. Guess the output of this JavaScript code?
302
+
303
+
```js
304
+
let a = { x:1 };
305
+
let b = { x:2 };
306
+
let c = { x:3 };
307
+
let d = { x:4 };
308
+
let e = { x:5 };
309
+
let arr = [a, b, c, d, e];
310
+
311
+
arr.forEach((obj) => (obj.x=obj.x*2));
312
+
313
+
console.log(a.x, b.x, c.x, d.x, e.x);
314
+
```
315
+
316
+
</h3>
317
+
</summary>
318
+
Answer:
319
+
320
+
2 4 6 8 10
321
+
322
+
The code is using the `forEach` method to iterate over an array of objects, and it is modifying the `x` property of each object by multiplying it by 2.
323
+
324
+
It's updating the original objects with `x*2` values.
325
+
326
+
So, the output of the code is 2 4 6 8 10.
327
+
328
+
</details>
329
+
330
+
<details>
331
+
<summary>
332
+
<h3>12. Guess the output of the JavaScript code?
333
+
334
+
```js
335
+
let num =0;
336
+
337
+
functiontest() {
338
+
var num =1;
339
+
return num;
340
+
}
341
+
342
+
console.log(test());
343
+
console.log(num);
344
+
```
345
+
346
+
</h3>
347
+
</summary>
348
+
Answer:
349
+
1
350
+
0
351
+
352
+
The code defines a global variable num with the value of 0 and then a function test which declares a local variable num with the value of 1 and returns it.
353
+
354
+
When test() is called, it first declares a local variable num with the value of 1.
355
+
356
+
Then the function return statement logs 1 on the console.
357
+
358
+
After that, it logs the value of global variable num which is 0.
359
+
360
+
Because the global and local variables have different scope and different memory allocation.
361
+
362
+
</details>
363
+
364
+
<details>
365
+
<summary>
366
+
<h3>13. Guess the output of the below JavaScript code?
367
+
368
+
```js
369
+
let obj = { name:"John", age:25 };
370
+
let newObj = { ...obj, age:30 };
371
+
372
+
console.log(obj.age);
373
+
console.log(newObj.age);
374
+
```
375
+
376
+
</h3>
377
+
</summary>
378
+
Answer:
379
+
25
380
+
30
381
+
382
+
The code creates an object obj with properties name and age. Then it creates a new object newObj using the spread operator to copy the properties of obj and then it updates the age property to 30.
383
+
384
+
The spread operator `...` creates a new object with properties copied from the original object.
385
+
386
+
So, the first console.log statement logs the value of age property of obj which is `25`.
387
+
388
+
And, the second console.log statement logs the value of age property of newObj which is `30`.
0 commit comments