What is the Output of this Code?
Given the following method calls in JavaScript, what is the output?
Copied to clipboard! Playground
const circle = {
radius: 10,
diameter() {
return this.radius * 2;
},
perimeter: () => 2 * Math.PI * this.radius
};
circle.diameter();
circle.perimeter();
The correct answer is NaN
. circle.diameter
executes without the problem but perimeter
is defined as an arrow function. Arrow functions donβt bind their own context which means this.radius
will be undefined
. And 2 * Math.PI * undefined
will be equal to NaN
.
Resources:
π More Webtips
Master the Art of Frontend
- Access 100+ interactive lessons
- Unlimited access to hundreds of tutorials
- Prepare for technical interviews