Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 14, 2021 11:43 am GMT

JavaScript question Day 3

What's the output ?

const shape = {  radius: 10,  diameter() {    return this.radius * 2;  },  perimeter: () => 2 * Math.PI * this.radius,};console.log(shape.diameter());console.log(shape.perimeter());
  • A: 20 and 62.83185307179586
  • B: 20 and NaN
  • C: 20 and 63
  • D: NaN and 63

Answer: B

Note that the value of diameter is a regular function, whereas the value of perimeter is an arrow function.

With arrow functions, the this keyword refers to its current surrounding scope, unlike regular functions! This means that when we call perimeter, it doesn't refer to the shape object, but to its surrounding scope (window for example).

There is no value radius on that object, which returns NaN.


Original Link: https://dev.to/soorajs98/javascript-question-day-3-3ahk

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To