JavaScript Intermediate Interview Questions

Here we are discussing few Intermediate level javascript interview questions but before going to intermediate level question you need to read basic javascript interview question.

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


+function() { 
    var a = b = 10; 
}();
 
console.log( typeof a ); 

undefined
Outside of the IIFE, the value of a is undefined and the type of undefined is undefined.

2. What will be the output of the following code?


var a = 10;
var b = a++;

console.log( b, a );

10, 11
Due to postfix this expression first a will store after that increment operation will perform. So before Increment operation a store in b and then next time it will increment; It will work like this.
a = a;
b = a;
a = a + 1;

3. What will be the output of the following code and the right solution?


function msg() {
      setTimeout(function() {
          console.log(this.name);
      }, 100);
}

msg.call({name: 'xyz'});

undefined
setTimeout is the browser api and so by default the current this or context is window. Inside msg this is passed object by due to setTimeout that loose current this.

Solution



function msg() {
     var self = this; 	
      setTimeout(function() {
          console.log(self.name);
      }, 100);
}

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


function msg() { return “Hi”; }

console.log( typeof msg() + " and " + typeof new msg() );

string and object
function msg returning a string value so first console print string when we use new then we are creating an object of constructor function so that way second console will return object.

5. What will be the output of the following code?


var ex1 = “” * 2;
var ex2 = “1” * 3;  
var ex3 = Math.sqrt(-2); 
var ex4 = 0/0; 

console.log( ex1, ex2, ex3, ex4 );

0, 3, NaN, NaN
The empty string converts into 0 so that way first is 0, same in the second case one will convert into 1 so second will be 3, third is indeterminate in math so javascript trait NaN, forth of negative value square root is invalid in maths so again js treat as NaN.

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


function Msg( mid, msg ) {
    this.mid = mid;
    this.msg = msg;
} 

Msg.prototype.msgOwner = function(name) {
    this.name = name;
}

var msg = new Msg(1, “hi”);

for (var k in msg) console.log(k);
msg.msgOwner(“xyz”);
for (var k in msg) console.log(k);

mid, msg, msgOwner
mid, msg, msgOwner, name
for in print all Msg constructor keys but name property will assign after msgOwner call that way in second time name property created.

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


msg();
var msg = function() {
 console.log(“Hi”); 
}

TypeError
msg called before declaration. In run time msg variable have undefined value and when we call undefined than it will show an error.

8. What will be the output of the following code?


function Msg() {}
console.log( Msg.prototype.constructor );

Msg
ConstructorFunction.prototype.constructor alway point to ConstructorFunction.

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


var obj = { name: “xyz” };
var o = Object.create(obj); 
o.name = “abc”

console.log(o.name, o.__proto__.name);

abc, xyz
object.create assign value inside __proto__ object so first name is own property of o object and second name is obj object.

10. What will be the output of the following code?


(function () {
      var msg = "";	
      msg += ("first");

      setTimeout(function() {
          msg += ("second");
          console.log("Inner :-", msg);
      }, 0);

      msg += ("third");
      console.log("Outer :-", msg);
}());

firstthird
firstthirdsecond
setTimeout is browser api and its execution happen inside queue and other operation happen inside stack so outer console execute before finish timeout function.

About the Author: Pankaj Bisht