0


while I was going through concepts related to JavaScript functions, I came up with problem which I'm unable to figure it out. I use following codes i.e one with return as a function and the other as a simple function as follows

function plus(a, b) {
   return(
       console.log(a+b),
       console.log(this),
       console.log(arguments)
   )
}
plus(4,5);

and

function plus(a, b) {      
       console.log(a+b),
       console.log(this),
       console.log(arguments)
}
plus(4,5)

But when I run both I'm not able to figure it out since both results same in console. So I just want to know when should I use return as function.? and what is its main purpose? I've seen answers here but those related to return entire function or objects but didn't find specific answer to return as a function. So please help me in it.

5
  • 1
    You don't return a function here, you return undefined in both cases. Commented Jan 16, 2015 at 1:08
  • @zerkms That's return "as a function" not "return a function", by which the OP must mean calling return with a () enclosed list of statements. Commented Jan 16, 2015 at 1:10
  • @MichaelBerkowski okay. And a list of "expressions" Commented Jan 16, 2015 at 1:10
  • console.log(a+b, this, arguments), no return Commented Jan 16, 2015 at 1:11
  • @MichaelBerkowski Yes that's what I exactly meant Commented Jan 16, 2015 at 1:13

2 Answers 2

5
return(
       console.log(a+b),
       console.log(this),
       console.log(arguments)
   )

statement means:

return the argument that is represented by (...) expression. Here parentheses is not a part of a return keyword syntax, but is a standalone operator used in conjunction with operator ,.

The (1, 2, 3) expression after evaluating returns the value of the last expression.

In your case it's console.log(arguments) which returns undefined.

References:

Sign up to request clarification or add additional context in comments.

9 Comments

Would arguments not be the arguments Array-like Object?
@PHPglue not sure what you mean
You say it's undefined when it would be a and b in an Array-like Object.
@zerkms how console.log(arguments) undefined when it gives array as PHPglue mentioned?
@PHPglue: console.log call returns undefined
|
1

This

function plus(a, b) {
   return(
       console.log(a+b),
       console.log(this),
       console.log(arguments)
   )
}

is an equivalent of this:

function plus(a, b) {
   var r = (
       console.log(a+b),
       console.log(this),
       console.log(arguments)
   );
   return r; 
}

or simply this (EDIT, but is wrong, see zerkms comment ):

function plus(a, b) {
   var r = 
       console.log(a+b),
       console.log(this),
       console.log(arguments);
   return r; 
}

where ,, is an expression - compute all parts delimited by commas with result of last expression.

1 Comment

"or simply this:" --- unless it's syntactically incorrect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.