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
<li><b>Reason</b> : 'a' and 'b' both are pointing to the same reference.</li>
1420
-
</ul>
1421
-
</details>
1422
-
1423
-
**[:top: Scroll to Top](#javascript-interview-questions)**
1424
-
1425
-
**4. What will be the output**
1426
-
```js
1427
-
for(var i =0; i <10; i++){
1428
-
setTimeout(function(){
1429
-
console.log("value is "+ i);
1430
-
})
1431
-
}
1432
-
```
1433
-
<details>
1434
-
<summary><b>View Answer</b></summary>
1435
-
<ul>
1436
-
<li><b>Output</b> : 10 times, "value is 10"</li>
1437
-
<li><b>Reason</b> : "var" has a function scope, and there will be only one shared binding for the iterations. By the time the setTimeout function gets executed, the for loop has already completed and the value of the variable i is 10.</li>
1438
-
</ul>
1439
-
</details>
1440
-
1441
-
**[:top: Scroll to Top](#javascript-interview-questions)**
1442
-
1443
-
**5. What will be the output**
1444
-
```js
1445
-
for(let i =0; i <10; i++){
1446
-
setTimeout(function(){
1447
-
console.log("value is "+ i);
1448
-
})
1449
-
}
1450
-
```
1451
-
<details>
1452
-
<summary><b>View Answer</b></summary>
1453
-
<ul>
1454
-
<li><b>Output</b> : 10 times "value is" followed by the value of i in each iteration, from 0 to 9</li>
1455
-
<li><b>Reason</b> : "let" has a block scope, and a new binding will be created for each iteration. Here, a new variable i is created and has a different value for each iteration of the loop.</li>
1456
-
</ul>
1457
-
</details>
1458
-
1459
-
**[:top: Scroll to Top](#javascript-interview-questions)**
1460
-
1461
-
**6. What will be the output**
1462
-
```js
1463
-
functionhello() {
1464
-
console.log("1");
1465
-
setTimeout(() => {
1466
-
console.log("2");
1467
-
})
1468
-
console.log("3");
1469
-
}
1470
-
hello();
1471
-
```
1472
-
<details>
1473
-
<summary><b>View Answer</b></summary>
1474
-
<ul>
1475
-
<li><b>Output</b> : "1" followed by "3", and then after a small delay, "2"</li>
1476
-
<li><b>Reason</b> : console.log("1") statement logs "1" to the console. Then setTimeout() function is set to execute the callback function in the next event loop iteration and logs "3" to the console.</li>
1477
-
</ul>
1478
-
</details>
1479
-
1480
-
**[:top: Scroll to Top](#javascript-interview-questions)**
1481
-
1482
-
**7. What will be the output**
1483
-
```js
1484
-
let f ="8";
1485
-
let a =1;
1486
-
console.log((+f)+a+1);
1487
-
```
1488
-
<details>
1489
-
<summary><b>View Answer</b></summary>
1490
-
<ul>
1491
-
<li><b>Output</b> : 10</li>
1492
-
<li><b>Reason</b> : The expression (+f) is a shorthand way to convert the string value of f to a number. Therefore, (+f) evaluates to 8.</li>
1493
-
</ul>
1494
-
</details>
1495
-
1496
-
**[:top: Scroll to Top](#javascript-interview-questions)**
1497
-
1498
-
**8. What will be the output**
1499
-
```js
1500
-
let a =10;
1501
-
if(true){
1502
-
let a =20;
1503
-
console.log(a, "inside");
1504
-
}
1505
-
console.log(a, "outside");
1506
-
```
1507
-
<details>
1508
-
<summary><b>View Answer</b></summary>
1509
-
<ul>
1510
-
<li><b>Output</b> : 20, "inside" and 10, "outside"</li>
1511
-
<li><b>Reason</b> : The variable "a" declared inside "if" has block scope and does not affect the value of the outer "a" variable.</li>
1512
-
</ul>
1513
-
</details>
1514
-
1515
-
**[:top: Scroll to Top](#javascript-interview-questions)**
1516
-
1517
-
**9. What will be the output**
1518
-
```js
1519
-
var a ="xyz";
1520
-
var a ="pqr";
1521
-
console.log(a)
1522
-
```
1523
-
<details>
1524
-
<summary><b>View Answer</b></summary>
1525
-
<ul>
1526
-
<li><b>Output</b> : "pqr"</li>
1527
-
<li><b>Reason</b> : Both the variables are declared using "var" keyword with the same name "a". The second variable declaration will override the first variable declaration.</li>
1528
-
</ul>
1529
-
</details>
1530
-
1531
-
**[:top: Scroll to Top](#javascript-interview-questions)**
1532
-
1533
-
**10. What will be the output**
1534
-
```js
1535
-
constarr1= [1, 2, 3, 4];
1536
-
constarr2= [6, 7, 5];
1537
-
constresult= [...arr1, ...arr2];
1538
-
console.log(result);
1539
-
```
1540
-
<details>
1541
-
<summary><b>View Answer</b></summary>
1542
-
<ul>
1543
-
<li><b>Output</b> : [1, 2, 3, 4, 6, 7, 5]</li>
1544
-
<li><b>Reason</b> : Spread operator (...) concatenates the two arrays into "result" array.</li>
1545
-
</ul>
1546
-
</details>
1547
-
1548
-
**[:top: Scroll to Top](#javascript-interview-questions)**
<li><b>Reason</b> : Spread operator (...) copies all the properties from person1 into person2.</li>
1561
-
</ul>
1562
-
</details>
1563
-
1564
-
**[:top: Scroll to Top](#javascript-interview-questions)**
1565
-
1566
-
**12. What will be the output**
1567
-
```js
1568
-
console.log(5<6<7);
1569
-
```
1570
-
<details>
1571
-
<summary><b>View Answer</b></summary>
1572
-
<ul>
1573
-
<li><b>Output</b> : true</li>
1574
-
<li><b>Reason</b> : In JavaScript, the < operator evaluates expressions from left to right. First, the expression 5 < 6 is evaluated, resulting in true because 5 is less than 6. Then, the expression true < 7 is evaluated. In this case, JavaScript performs type coercion and converts true to the number 1. Therefore, the expression becomes 1 < 7, which is true.</li>
1575
-
</ul>
1576
-
</details>
1577
-
1578
-
**[:top: Scroll to Top](#javascript-interview-questions)**
1579
-
1580
-
**13. What will be the output**
1581
-
```js
1582
-
console.log(7>6>5);
1583
-
```
1584
-
<details>
1585
-
<summary><b>View Answer</b></summary>
1586
-
<ul>
1587
-
<li><b>Output</b> : false</li>
1588
-
<li><b>Reason</b> : In JavaScript, the > operator evaluates expressions from left to right. First, the expression 7 > 6 is evaluated, resulting in true because 7 is greater than 6. Then, the expression true > 5 is evaluated. In this case, JavaScript performs type coercion and converts true to the number 1. Therefore, the expression becomes 1 > 5, which is false.</li>
1589
-
</ul>
1590
-
</details>
1591
-
1592
-
**[:top: Scroll to Top](#javascript-interview-questions)**
1593
-
1594
-
**14. What will be the output**
1595
-
```js
1596
-
console.log(0==false);
1597
-
console.log(1==true);
1598
-
```
1599
-
<details>
1600
-
<summary><b>View Answer</b></summary>
1601
-
<ul>
1602
-
<li><b>Output</b> : true, true</li>
1603
-
<li><b>Reason</b> : The == operator converts operands to a common type before making the comparison. In both the cases, the boolean value will be converted to a number, i.e., false is converted to 0 and true is converted to 1. So, the expression 0 == false is equivalent to 0 == 0 and 1 == true is equivalent to 1 == 1.</li>
1604
-
</ul>
1605
-
</details>
1606
-
1607
-
**[:top: Scroll to Top](#javascript-interview-questions)**
1608
-
1609
-
**15. What will be the output**
1610
-
```js
1611
-
console.log([11, 2, 31] + [4, 5, 6]);
1612
-
```
1613
-
<details>
1614
-
<summary><b>View Answer</b></summary>
1615
-
<ul>
1616
-
<li><b>Output</b> : "11,2,314,5,6"</li>
1617
-
<li><b>Reason</b> : The + operator is used for both addition and string concatenation. When you try to concatenate two arrays using the + operator, the arrays are converted to strings and then concatenated together. In this case, the arrays [11, 2, 31] and [4, 5, 6] are converted to strings as "11,2,31" and "4,5,6" respectively. Then, the two strings are concatenated, resulting in "11,2,314,5,6".</li>
1618
-
</ul>
1619
-
</details>
1620
-
1621
-
**[:top: Scroll to Top](#javascript-interview-questions)**
0 commit comments