Why does this function returns the last argument when called right after definition

fatihsolhan
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?

Nick Parsons

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does this definition returns a function?

Why does a function called inside a inline function not require definition?

Why is the copy constructor not called when the function returns?

Why does the copy constructor (of a C++ class) get called when a function returns?

Why does this simple Js function not return a value/object when () is added after the definition?

Why does deallocation function called with one argument instead of two?

Does feof() work when called after reading in last line?

Why does Haskell insist on removing the last argument of a function?

Function returns value when called in console, but won't return when called after function declaration

Why the function argument returns undefined when using props?

Why Could Not Deduce Template Argument When Function Returns a Struct Template

Why does python evaluate the arguments of a function when defined and not when called?

Why does it show that my function returns an int when it returns a char*?

Why does my JavaScript function work but displays "undefined" right after?

Why does function not get executed when called through web server?

Why does the defaultWriteObject function have to be called first when writing into an ObjectOutputStream?

Why does PowerShell code behave different when called from a function?

Why does "this" work differently when function is called with and without "new"

Why does the program crash when the function is called in c?

Why does the program skip taking input when function is called?

Why does std::begin behave differently when called in a nested function

How to understand a function definition when passing it as an argument

When input() is used as function argument, why does it run before main()

Why does compiler throw an error when we declare an extra template argument and not used in definition?

Why xargs does not process the last argument?

Avoid two time argument definition when passing function as argument typescript

C++: Why Position constructor are called when the argument type does not match?

Why does gcc infer the type from the callback function's last argument here?

Why does 'this' return 'undefined' when referred in an arrow function but doesn't when called on an anonymous function?