Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
Arrow Function this prints var but does not print let variable

I have this code

var key = "outer value";
let obj = {
  key:"obj value",
  func:()=> this.key,
}

console.log(obj.func());

But when the var is changed to let, the result is undefined. Please guide.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

let key = "outer value";
let obj = {
  key:"obj value",
  func:()=> key,
}

console.log(obj.func()); //--> Outer Value


var key = "outer value";
let obj = {
  key:"obj value",
  func:()=> key,
}

console.log(obj.func()); //--> Outer Value

The answer would be same even if this keyword is not used

var key = "outer value";
let obj = {
  key:"obj value",
  func:()=> key,
}

console.log(obj.func());

about 4 years ago · Juan Pablo Isaza Report

0

An arrow function does not have its on this, It always uses parent this

Your code works because the key is global scope(this)

let key = "outer value";
let obj = {
  key:"obj value",
  func:()=> key,
}
console.log(obj.func());

This will not work because key is local scope not inside this

function test() {
  var key = "outer value";
  let obj = {
    key: "obj value",
    func: () => this.key,
  };

  console.log(obj.func()); // undefined
}
test();

This Will work

function test() {
  this.key = "outer value";
  let obj = {
    key: "obj value",
    func: () => this.key,
  };

  console.log(obj.func()); // outer value
}
test();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!