Top Four Trickiest JavaScript Interview Questions For 2018

As the New Year is approaching, many of you might be looking for new opportunity in different companies. Today, I will be discussing the top 4 trickiest JavaScript interview questions for 2018. As I have seen the trends in 2017 and the interview questions asked in this year, I am sure in the next year, if you are facing any JavaScript interview, you will definitely go through at least one of these questions.

Let's get started!!!

Q-1 Given below is a function written in JavaScript as well as in C#.

  1. function printTheCount() {   
  2.     for (var i = 0; i < 5; i++) { 
  3.             console.log("value of i = " + i);  
  4.             for (var i = 0; i < 5; i++)  
  5.         }  
  6.     console.log("Finally value of i = " + i); 
  7.           
  8.     }
  9.   
  10.     printTheCount(); 
  11. }  

As you can see, the same code is written in both, JavaScript and C#. So what is the output in both the cases?

ANS

Output in JavaScript will be,

value of i = 0
value of i = 1
value of i = 2
value of i = 3
value of i = 4
Finally value of i = 5

Whereas, in C#, it will be a compile time error.

As in JavaScript, the scope of the variable declared in a for loop is different than C#.

Unlike C#, in JavaScript, the variable in scope is the whole function. So, the variable is available outside the for loop in the last line also.

  1. console.log("Finally value of i = " + i);  

In C#, it will give a compile time error as “I” doesn’t exist in the last line.

  1. console.WriteLine("Finally value of i = " + i);  

Q-2 - Find the length of the following JavaScript code.

  1. var array = [];  
  2. array[0] = "abc";  
  3. array[1] = 123;  
  4. array[3] = true;  
  5. console.log(array[0]);  
  6. console.log(array[1]);  
  7. console.log(array[2]);  
  8. console.log(array[3]);  
  9. console.log(array[4]);  
  10. console.log(array.length);  

What is the output of the above code?

ANS

  • abc
  • 123
  • undefined
  • true
  • undefined
  • array length is 4

Surprised by the answer? You can try it yourself here.

The length of an array in JavaScript is actually “maximum index” + 1. Here, you can see that while declaring, we declared 3 as the highest index in the array, so the length of the array is 4.

Whatever index is not initialized, are undefined in JavaScript, without any error.

Q-3 - What will happen if the number of passing arguments to a function is not correct in JavaScript?

Let's see the code to understand the question properly.

  1. function Add(x, y) {  
  2.     console.log(x + y);  
  3. }  
  4. Add(1, 2, 3, 4);  
  5. Add(1);  
  6. Add(“1”);  

ANS

  • 3
  • NaN
  • 1Undefined

It doesn’t matter in JavaScript how many arguments you are passing to a function. If you are passing more arguments than expected, then it will capture only the required number of arguments from the left side.

In the Add(1,2,3,4); the output is “3”. This is the addition of 1 and 2. Here, 3 and 4 are not captured by the function itself.

In case you are passing less than the expected arguments to a function, then the remaining arguments are considered as undefined. In Add(1); only one argument is passed, so the other argument is considered as Undefined. The output of “1+ undefined “ is NaN.

In case of Add(“1”); only one argument is passed to the function. That is of type string, so the output is “1” + “Undefined” i.e. 1Undefined.

You can check the example here.

Q-4 - Is JavaScript value type or reference type?

In all the object-oriented languages, there is a concept of value type or reference type variables. But does JavaScript follows these concepts? As you know JavaScript follows the OOPs concept, then how the variables behave in JavaScript?

ANS

JavaScript doesn’t follow either Value type or Reference type. It follows a different concept called call-by-sharing.

Let's see the example below to understand better.

  1. function changeObject(x)  
  2. {  
  3.     x = {  
  4.         place: "inside"  
  5.     };  
  6.     console.log("inside changeObject place : " + x.place);  
  7. }  
  8.   
  9. function changeMember(x)  
  10. {  
  11.     x.place = "inside";  
  12.     console.log("inside changeMember place : " + x.place);  
  13. }  
  14. x = {  
  15.     place: "outside"  
  16. };  
  17. console.log("before changeObject place: " + x.place);  
  18. changeObject(x);  
  19. console.log("after changeObject place: " + x.place);  
  20. console.log("before changeMember place: " + x.place);  
  21. changeMember(x);  
  22. console.log("after changeMember place: " + x.place);  

Output

before changeObject place: outside
inside changeObject place : inside
after changeObject place: outside
before changeMember place: outside
inside changeMember place : inside
after changeMember place: inside

It's always passed by value in JavaScript, but for objects, the value of the variable is a reference. So, when you pass an object and change its variables, those changes persist outside of the function. This makes it look like pass by reference.

But, if you actually change the value of the object variable, you will see that the change does not persist, proving it really passes by value.

You can check the example here.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now