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

212
Views
Why does this function returns the last argument when called right after definition
function foo(x) {
  return x;
}
foo(1); // returns 1 as expected

function bar(x) {
  return x;
}(1,2,3); // returns 3

I couldn't understand why does bar function returns the last argument. Can anyone explain it to me?

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

0

Your bar() function is being interpreted as a function declaration and not as a function expression, as a result, the (1, 2, 3) is not the call syntax for functions (), but rather the grouping operator using the comma-operator, which returns the last value of 3 (which is then logged in the console). If it's on separate lines it may become clearer:

function bar(x) { // declares a function bar
  console.log("running"); // never logged
  return x;
}
// Uses the grouping operator `( )`, with the comma operator
(1,2,3); // returns 3

If you make bar() a function expression, then it will behave as expected (here I'm using the unary plus + to achieve that):

+function bar(x) {
  console.log("running"); // logged
  return x;
}(1,2,3); // returns 1

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!