0

What will be the output of the following JavaScript code and why?

author
kallyani
medium
0
6
const obj = {

name: "John",

outer() {

function inner() {

console.log(this.name);

}

inner();

}

};

obj.outer();
Answer

The output will be:

undefined


  • The method outer() is called using obj.outer(), so inside outer, this refers to obj.
  • However, inner() is a regular function, not a method of the object.
  • When inner() is called, it is executed without any object reference.
  • In JavaScript, for a normal function call, this refers to the global object (or undefined in strict mode).
  • Since the global object does not have a name property, this.name becomes undefined.

Click to Reveal Answer

Tap anywhere to see the solution

Revealed

Comments0