DEV Community

abhishek
abhishek

Posted on

javascript return?

function e(){
  return 4, false;
}

e() 
// e () -> false
function f() {
  return 4 + 4, true;
}
f()
// f() -> true
Enter fullscreen mode Exit fullscreen mode

Shouldn't I expect it to return 4 or sum of 4 + 4. Why every time my function e() and function f() return 2nd value?

Top comments (5)

Collapse
 
stereobooster profile image
stereobooster •

The comma operator (,) evaluates each of its operands (from left to right) and returns the value of the last operand.

developer.mozilla.org/en-US/docs/W...

Collapse
 
johndoesup profile image
abhishek •

Thank you. I actually got confused on left to right and right to left evaluate thing.

Collapse
 
anderspersson profile image
Anders Persson •

return only returns 1 value, and it vould be the last,
if you like to return more then 1 value you can do this as a array

function e() {
return [4,false];
}

if you then like to get first part just type
e()[0];

Collapse
 
johndoesup profile image
abhishek •

thank you it was really helpful. I saw this code in react, they had this long complex return with , so I thought what the use of it?

Collapse
 
coderslang profile image
Coderslang: Become a Software Engineer •

what's the desired behavior of these functions?