Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

69
Views
Why are anonymous functions passed into variables?

So, I'm using freecodecamp and it says that the following function is anonymous:

const myFunc = () => {
  const myVar = "value";
  return myVar;
}

console.log(myFunc.name);

Well, how come it's anonymous if its name is clearly "myFunc"?

7 months ago · Santiago Gelvez
1 answers
Answer question

0

You are setting myFunc to reference an anonymous function. myFunc has a name, the anonymous function it references does not.

const myFunc = () => {
  const myVar = "value";
  return myVar;
}

// This returns myFunc name
console.log(myFunc.name);

// This returns anonymous function's name which is blank
console.log(
(() => {
  const myVar = "value";
  return myVar;
}).name
);

7 months ago · Santiago Gelvez Report
Answer question
Find remote jobs