What is the output of the following? “`js const obj = { value: 42, getValue: function() { return this.value; }, getValueArrow: () => this.value }; console.log(obj.getValue()); console.log(obj.getValueArrow()); “`

JavaScript Developer Hard

JavaScript Developer — Hard

What is the output of the following? “`js const obj = { value: 42, getValue: function() { return this.value; }, getValueArrow: () => this.value }; console.log(obj.getValue()); console.log(obj.getValueArrow()); “`

Key points

  • Regular functions have their own `this` context
  • Arrow functions do not have their own `this` context
  • Arrow functions lexically bind `this` to the surrounding code
  • The `getValueArrow` function returns `undefined` because `this.value` is undefined in the arrow function

Ready to go further?

Related questions