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

220
Views
Why function called by function name not working when function is created by assigning it to a variable?

// I created two function in JavaScript

// **Function 1**

function add(a, b) {
  return a + b;
}

// To call above function I use and it worked as expected
add(2, 3);




// **Function 2**

let x = function mul(a, b) {
  return a * b;
}

// To call above function I use two ways
x(2, 3);

mul(2, 3); // This won't work. Why?

Why the mul(2,3); throwing an error?

mul(2,3) is the name of the function, it should be used to call the function.

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

0

This shows you that in JavaScript, functions are also first-class values. In the add function you have up there, the name of the function: add, is actually a variable that a function definition is stored inside of it. For the second example, mul is not registered as a variable in the scope (list of variables) in the program's memory, rather, what you have there is a function expression, wherein you assign a function definition or say function value to a variable that is x in your case, omitting the mul in that function definition is in fact valid.

The error from calling mul is caused by trying to access mul in the scope (appears to be global) where it is not declared and is neither accessible from other scopes the global scope can access.

Scope (available variables to a program's memory) exists at different levels in JavaScript, two of which are global scope and function scope.

Within the scope of the mul function, mul is accessible there, as well as other variables in a scope that is at a higher level like add (present in global scope) because inner scopes can access outer scopes.

Whereas, mul as well as other variables declared within it cannot be accessible outside of mul's function scope, because outer scope cannot access inner scope.

The inner scopes in your snippet are those of add and mul function scopes, while the outer scope appears to be a global scope.

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!