Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 537e05f

Browse files
committed
new JS questions added
1 parent eb58231 commit 537e05f

File tree

1 file changed

+115
-1
lines changed

1 file changed

+115
-1
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Prepare for your next 2023 JavaScript interview with these tricky
44
githubPath: "https://github.com/Vasu7389/JavaScript-Interview-Questions-2023"
55
---
66

7-
<span style=" font-size: 0.8rem; border-bottom: 1px solid grey;"> Updated Feb 10, 2023 </span>
7+
<span style=" font-size: 0.8rem; border-bottom: 1px solid grey;"> Updated Feb 25, 2023 </span>
88

99
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).
1010

@@ -491,3 +491,117 @@ The third console.log statement logs the value of c which is `3`.
491491
In ES6+, you can use destructuring assignment to extract values from arrays and objects and assign them to variables in a concise way.
492492

493493
</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(typeof null);
501+
console.log(typeof undefined);
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.
530+
</h3>
531+
</summary>
532+
Answer:
533+
534+
```js
535+
function sumOfPositiveNumbers(numbers) {
536+
let sum = 0;
537+
for (let i = 0; i < numbers.length; i++) {
538+
if (numbers[i] > 0) {
539+
sum += numbers[i];
540+
}
541+
}
542+
return sum;
543+
}
544+
545+
// Example usage:
546+
const arr = [1, -2, 3, 4, -5, 6];
547+
console.log(sumOfPositiveNumbers(arr)); // Output: 14
548+
```
549+
550+
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.
559+
</h3>
560+
</summary>
561+
Answer:
562+
563+
```js
564+
function removeVowels(str) {
565+
const vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"];
566+
let newStr = "";
567+
for (let i = 0; i < str.length; i++) {
568+
if (!vowels.includes(str[i])) {
569+
newStr += str[i];
570+
}
571+
}
572+
return newStr;
573+
}
574+
575+
// Example usage:
576+
const str = "This is a test string with vowels";
577+
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.
589+
</h3>
590+
</summary>
591+
Answer:
592+
593+
```js
594+
function sortStrings(arr) {
595+
return arr.sort();
596+
}
597+
598+
// Example usage:
599+
const strings = ["apple", "banana", "cherry", "date", "elderberry"];
600+
console.log(sortStrings(strings)); // Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']
601+
```
602+
603+
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']`.
606+
607+
</details>

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /