Introduction
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#.
- function printTheCount() {
- for (var i = 0; i < 5; i++) {
- console.log("value of i = " + i);
- for (var i = 0; i < 5; i++)
- }
- console.log("Finally value of i = " + i);
- }
- printTheCount();
- }
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.
- 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.
- console.WriteLine("Finally value of i = " + i);
Q-2 - Find the length of the following JavaScript code.
- var array = [];
- array[0] = "abc";
- array[1] = 123;
- array[3] = true;
- console.log(array[0]);
- console.log(array[1]);
- console.log(array[2]);
- console.log(array[3]);
- console.log(array[4]);
- 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.
- function Add(x, y) {
- console.log(x + y);
- }
- Add(1, 2, 3, 4);
- Add(1);
- Add(“1”);
ANS
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 a 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.
- function changeObject(x)
- {
- x = {
- place: "inside"
- };
- console.log("inside changeObject place : " + x.place);
- }
-
- function changeMember(x)
- {
- x.place = "inside";
- console.log("inside changeMember place : " + x.place);
- }
- x = {
- place: "outside"
- };
- console.log("before changeObject place: " + x.place);
- changeObject(x);
- console.log("after changeObject place: " + x.place);
- console.log("before changeMember place: " + x.place);
- changeMember(x);
- 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.