Tricky javaScript interview questions

javascript-interview-questions-answers

Here I am going to discuss some really tricky javaScript interview questions.

1. What will be the output of the below code?

Ans.

This is the really tricky javaScript interview question for JavaScript developer. The output of this code will be 1undefined because here we have function statement function fun(){} inside if so it returns true. And here fun is function expression not a function statement, therefore, it will be undefined here.


2. Write a single line statement to remove all duplicate elements from below given array or any array

Ans.

To answer this question, I am going to use ES6 new features. ES6 has provided many new functions for Javascript Developers ease. Here I am going to discuss about Set().
As we all know that Set() contents only unique items. Please check below single line solution of above question.

If you run above answer then you get an array [1, 2, 3, 5, 7, 8].
In above answer, I have used one more new ES6 feature that is spread operator(…). For more detail, about Spread Operator you can check this link What is spread, operator?

3. How to create function like “add(1)(2)(3)” to add 1,2,3 and return 6?

Ans.

This is also a tricky javaScript interview question. Please check below code to answer this question.

4. What will be the output of the below code?

Ans.

This is also a tricky javaScript interview question. The code will alert 5″ five times because every time setTimeout will execute after execution of the loop.
Here timeout value is zero then still it will wait till the execution of a loop.

5. What is the difference between “forEach” and “map”?

Ans.

forEach does not return an array, It will modify the existing array only but map returns a new array and it does not modify existing array.
Please check below code to understand this.

6. What will be the output of the below code?

Ans.

Again this is tricky javaScript interview question. Here output will true. Because when you are assigning one array to another array it takes reference of the array. This is the reason when you modify any of them it refect to both arrays. Means both arrays will be same.

To overcome this problem you can you use Spread Operator or slice() method.

7. What will be the output of the below code?

Ans.

The Output of the above code will be false because else part will execute.
When we are using 7 > 6 > 5, means with firstly check 7 > 6 and it will give us true now this express will become like true > 5. When it checks true > 5 , it converts true into 1 and one is not greater than 5.

8. What is the default parameter to the function?

Ans.

The default parameter is a very important concept of javascript. When we will define default parameters then it takes that value only when you are not going to pass any value.
Check below example for clear understanding.

9. What would be the output of the following code?

Ans.

The Answer of the above code will be undefined because employeeId once again declare inside function, therefore, it will not take the outer scope. And we are printing before initialization.

These are some tricky javaScript interview questions.

Please follow and like us:

You May Also Like

About the Author: Sujeet Kumar

Sujeet is Web and Android Devloper with a soft spot for functional programming. I like to play cricket, regular workout.

Leave a Reply

Your email address will not be published. Required fields are marked *