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 Basics (Retired) Creating Reusable Code with Functions Review: Getting Information from Functions

Lucille Fox
Lucille Fox
881 Points

Why is my code returning undefined?

I am not grasping this lesson and in trying to understand it, I am running all the examples and questions through the console. But rather than receiving any answer I am seeing underfined in the console. What am I doing wrong?

function getDay() { return "Monday"; alert ("Calculating day"); return "Friday"; } var dayOfWeek = getDay(); undefined

2 Answers

Steven Parker
Steven Parker
231,269 Points

You appear to have the word "undefined" added to the end of the code, which is odd but it will not affect the value stored in "dayOfWeek".

None of this code writes anything to the console. So what is it you are doing when you see "undefined" in the console?

Lucille Fox
Lucille Fox
881 Points

Hi Stephen, thanks for your reply. This is what I am testing in the console ( I know the answer is Monday) I was just writing everything out so I can gain a better understanding of it. So imagine this is the js console: function getDay() { return "Monday"' alert("Calculating day"); return "Friday"; } var dayOfWeek = getDay():

The response from the console is "undefined" rather than Monday - I wondered why (I'm obviously really new and trying to gain an understanding. Thanks again.

Steven Parker
Steven Parker
231,269 Points

I think I get it now. You're typing that whole thing into the console, and the console is returning "undefined" since there's no return value associated with the statement(s). But your variable is being assigned.

So if you then type in "dayOfWeek" it will show you the value that was assigned to it.

You could also type in "getDay()" to run the function and see the return code it gives directly.

Priya Gurung
Priya Gurung
7,097 Points

Hi Lucille,

The reason why the console is returning undefined is because no value is being returned explicitly. If you simply invoked the function getDay, you would get "Monday".

ex: type this in to the DevTools console:

getDay(); // you would get "Monday"

When you invoke getDay and store the value into the variable dayOfWeek, you're actually running two operations here. One, a function call, which does return a value, and two, an assignment.. The console is assessing the last operation, assignment, and sees that nothing is being returned so prints out undefined.

If you were to write a similar function called printDay which looks like this:

function printDay() {
   console.log("Monday");
}

You would encounter another undefined once the function was invoked. Again this is due to nothing being returned.

Hope that makes sense!

Lucille Fox
Lucille Fox
881 Points

Thank you Priya it does :D