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 Feb 10, 2023 </span>
7
+
<spanstyle="font-size: 0.8rem; border-bottom: 1pxsolidgrey;"> Updated Feb 25, 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
@@ -491,3 +491,117 @@ The third console.log statement logs the value of c which is `3`.
491
491
In ES6+, you can use destructuring assignment to extract values from arrays and objects and assign them to variables in a concise way.
492
492
493
493
</details>
494
+
495
+
<details>
496
+
<summary>
497
+
<h3>17. What is the output of the below JavaScript code and why?
498
+
499
+
```js
500
+
console.log(typeofnull);
501
+
console.log(typeofundefined);
502
+
console.log(null===undefined);
503
+
console.log(null==undefined);
504
+
```
505
+
506
+
</h3>
507
+
</summary>
508
+
Answer:
509
+
510
+
```bash
511
+
object
512
+
undefined
513
+
false
514
+
true
515
+
```
516
+
517
+
`typeof null` returns object which is an error in JavaScript. This is a historical bug in the language that cannot be fixed without breaking existing code. So, to check for `null`, you should use `===` null instead of `typeof` operator.
518
+
519
+
typeof undefined returns undefined.
520
+
521
+
null === undefined is false because `null` and `undefined` are two distinct types in JavaScript.
522
+
523
+
null == undefined is true because `==` is the loose equality operator in JavaScript, which performs type coercion before comparison. In this case, both null and undefined are coerced to undefined before comparison, and since they both have the same value, the comparison returns true. However, it is generally recommended to use `===` instead of `==` to avoid unexpected behavior due to type coercion.
524
+
525
+
</details>
526
+
527
+
<details>
528
+
<summary>
529
+
<h3>18. Write a function in JavaScript that takes an array of numbers and returns the sum of all positive numbers in the array.
The `sumOfPositiveNumbers` function takes an array of numbers as its parameter and initializes a variable sum to `0`. It then loops through each element of the array and checks if the number is greater than 0. If the number is positive, it adds the number to the sum. Finally, it returns the sum of all positive numbers in the array.
551
+
552
+
In the example usage, we pass an array `[1, -2, 3, 4, -5, 6]` to the sumOfPositiveNumbers function. The function returns the sum of all positive numbers in the array, which is `14`.
553
+
554
+
</details>
555
+
556
+
<details>
557
+
<summary>
558
+
<h3>19. Write a function in JavaScript that takes a string as input and returns a new string with all the vowels removed.
console.log(removeVowels(str)); // Output: Ths s tst strng wth vwls
578
+
```
579
+
580
+
The `removeVowels` function takes a string as its parameter and initializes an array vowels containing all the vowels. It then loops through each character of the input string and checks if the character is not present in the vowels array. If the character is not a vowel, it adds the character to the `newStr` string. Finally, it returns the newStr string with all the vowels removed.
581
+
582
+
In the example usage, we pass a string 'This is a test string with vowels' to the removeVowels function. The function returns a new string with all the vowels removed, which is 'Ths s tst strng wth vwls'.
583
+
584
+
</details>
585
+
586
+
<details>
587
+
<summary>
588
+
<h3>20. Write a function in JavaScript that takes an array of strings as input and returns a new array with the strings sorted in alphabetical order.
The `sortStrings` function takes an array of strings as its parameter and uses the `sort()` method to sort the array in alphabetical order. The sort() method sorts the elements of an array in place and returns the sorted array. By default, the sort() method sorts the array elements in ascending order based on the `Unicode` values of the characters. Therefore, for strings, it sorts them in alphabetical order.
604
+
605
+
In the example usage, we pass an array of strings `['apple', 'banana', 'cherry', 'date', 'elderberry']` to the sortStrings function. The function returns a new array with the strings sorted in alphabetical order, which is `['apple', 'banana', 'cherry', 'date', 'elderberry']`.
0 commit comments