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 19, 2023 </span>
7
+
<spanstyle="font-size: 0.8rem; border-bottom: 1pxsolidgrey;"> Updated Feb 10, 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
@@ -390,3 +390,104 @@ And, the second console.log statement logs the value of age property of newObj w
390
390
It doesn't affect the original object `obj`.
391
391
392
392
</details>
393
+
394
+
<details>
395
+
<summary>
396
+
<h3>14. Guess the output of the below JavaScript code?
397
+
398
+
```js
399
+
constadd= (a=1, b=2) => a + b;
400
+
console.log(add());
401
+
console.log(add(5));
402
+
console.log(add(undefined, 10));
403
+
```
404
+
405
+
</h3>
406
+
</summary>
407
+
408
+
Answer:
409
+
410
+
```bash
411
+
3
412
+
7
413
+
11
414
+
```
415
+
416
+
The code defines a function add which takes two parameters a and b and returns the sum of both. It uses default parameters to assign default values 1 to a and 2 to b if they are not provided.
417
+
418
+
So, the first console.log statement logs the result of add() which is 1 + 2 = 3 as both the parameters are not provided and default values are used.
419
+
420
+
The second console.log statement logs the result of add(5) which is 5 + 2 = 7 as only the first parameter is provided and the default value of b is used.
421
+
422
+
The third console.log statement logs the result of add(undefined, 10) which is 1 + 10 = 11 as the first parameter is provided as undefined and it takes the default value 1 and the second parameter is provided as 10.
423
+
424
+
</details>
425
+
426
+
<details>
427
+
<summary>
428
+
<h3>15. Guess the output of the below JavaScript code?
429
+
430
+
```js
431
+
constname="John";
432
+
constage=25;
433
+
434
+
constuser= { name, age };
435
+
console.log(user);
436
+
```
437
+
438
+
</h3>
439
+
</summary>
440
+
441
+
Answer:
442
+
443
+
```js
444
+
{ name:"John", age:25 }
445
+
```
446
+
447
+
The code defines two variables name and age with values "John" and 25 respectively.
448
+
449
+
Then, it uses `object literal` notation to create an object user with properties `name` and `age` and the values are assigned from the variables name and age respectively.
450
+
451
+
So, the `console.log` statement logs the user object which is `{ name: "John", age: 25 }`.
452
+
453
+
In `ES6+`, you can use object literal notation to create objects with properties using the same name as the variables with the values assigned to them.
454
+
455
+
</details>
456
+
457
+
<details>
458
+
<summary>
459
+
<h3>16. Guess the output of the below JavaScript code?
460
+
461
+
```js
462
+
constarr= [1, 2, 3];
463
+
const [a, b, c] = arr;
464
+
465
+
console.log(a);
466
+
console.log(b);
467
+
console.log(c);
468
+
```
469
+
470
+
</h3>
471
+
</summary>
472
+
473
+
Answer:
474
+
475
+
```bash
476
+
1
477
+
2
478
+
3
479
+
```
480
+
481
+
The code defines an array arr with values [1, 2, 3].
482
+
483
+
Then, it uses `destructuring assignment` to extract the values from the array `arr` and assign them to variables `a`, `b`, and `c` respectively.
484
+
485
+
So, the first console.log statement logs the value of a which is `1`.
486
+
487
+
The second console.log statement logs the value of b which is `2`.
488
+
489
+
The third console.log statement logs the value of c which is `3`.
490
+
491
+
In ES6+, you can use destructuring assignment to extract values from arrays and objects and assign them to variables in a concise way.
0 commit comments