What is the output of the following? “`js const obj = { data: [1, 2, 3], double() { return this.data.map(function(n) { return n * this.multiplier; }); }, multiplier: 2 }; console.log(obj.double()); “`

JavaScript Professional Medium

JavaScript Professional — Medium

What is the output of the following? “`js const obj = { data: [1, 2, 3], double() { return this.data.map(function(n) { return n * this.multiplier; }); }, multiplier: 2 }; console.log(obj.double()); “`

Key points

  • The use of `this` inside a nested function can lead to unexpected behavior
  • Arrow functions can help maintain the correct context of `this`
  • Understanding scope and context is crucial in JavaScript

Ready to go further?

Related questions