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
Finally value of i = 5
value of i = 1
value of i = 2
value of i = 3
value of i = 4
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);
- 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.
Sagar Pandurang KapPosted Dec 21, 2017, 3:54 AM
Useful article.Thnx.Informative..