Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

Question on return statement in a function

I came across this question in one of the forum: Q: Will they both return the same thing? Why or why not?

function foo1() { return { bar: "hello" }; }

function foo2() { return { bar: "hello" }; }

I tried it in chrome console but I am getting different results on logging two functions result. I am not able to figure out why? Please help!

console.log(foo1()) returns - Object {bar: "hello"} console.log(foo2()) returns - undefined

6 Answers

Steven Parker
Steven Parker
229,644 Points

They look, and function, identically for me. Maybe you had a typo when you tried them.

I cut-and-pasted directly from your question above.

You say this was asked before? Can you provide a link to the original question?

alastair cooper
alastair cooper
30,617 Points

This answer comes from the following webpage where the question is asked: https://www.toptal.com/javascript/interview-questions

I tried to understand it so I could explain it to you better, but it is a little complicated. The way you have written it above, it will work, but if it is written like so:

function foo1()
{
  return {
      bar: "hello"
  };
}

function foo2()
{
  return
  {
      bar: "hello"
  };
}

then it will return immediately after the return statement at the beginning of foo2

The reason for this has to do with the fact that semicolons are technically optional in JavaScript (although omitting them is generally really bad form). As a result, when the line containing the return statement (with nothing else on the line) is encountered in foo2(), a semicolon is automatically inserted immediately after the return statement.

No error is thrown since the remainder of the code is perfectly valid, even though it doesn’t ever get invoked or do anything (it is simply an unused code block that defines a property bar which is equal to the string "hello").

This behavior also argues for following the convention of placing an opening curly brace at the end of a line in JavaScript, rather than on the beginning of a new line. As shown here, this becomes more than just a stylistic preference in JavaScript.

Sorry if this is not very clear

Alastair

Steven Parker
Steven Parker
229,644 Points

That extra newline tells the whole story. It wasn't present in the question above!

The curly brace argument only applies to object literals, for code blocks putting them on the next line is fine.

Hi Alastair and Steven, Thanks for replying back but what I am trying understand is why the placement of curly braces is making difference in the output. What is the right way to place the curly braces in the program. One of the video in Treehouse explains that Javascript ignores the blank spaces then why this difference in placement of curly brace.

alastair cooper
alastair cooper
30,617 Points

If the opening curly brace is not on the same line, then the javascript compiler will add a semi-colon. This means the function finishes on that line and the rest is not read. If you add the brace at the end of the line then it will not insert the semi-Colon and the function is fine

Steven Parker
Steven Parker
229,644 Points

Alastair was familiar with the original question, and he explained it. Note that when you re-type the functions on a single line, the problem goes away.

The catch is that a return statement doesn't have to have a value, so if you skip to the next line, it returns from the function without a value (showing "undefined").

But when you put any part of a value to return, in this case the opening brace of a literal object, it will return the entire value. The fact that it's a brace is not important. What matters is whether or not the return is on a line by itself.

Cool! Thanks guys. Got it now :)