InterviewHubPRO
Section Overview
Q360

Question 360 of 521

Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

69%
JavaScript Internals (521 Qs)•Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

What is the output of below function calls

Code snippet:

javascript
  const circle = {
    radius: 20,
    diameter() {
      return this.radius * 2;
    },
    perimeter: () => 2 * Math.PI * this?.radius,
  };
javascript
  console.log(circle.diameter());
  console.log(circle.perimeter());

Output:

The output is 40 and NaN. Remember that diameter is a regular function, whereas the value of perimeter is an arrow function. The this keyword of a regular function(i.e, diameter) refers to the surrounding scope which is a class(i.e, Shape object). Whereas this keyword of perimeter function refers to the surrounding scope which is a window object. Since there is no radius property on window objects it returns an undefined value and the multiple of number value returns NaN value.