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 DOM Scripting By Example Improving the Application Code Refactor 1: Create List Items

Trevor Maltbie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trevor Maltbie
Full Stack JavaScript Techdegree Graduate 17,021 Points

function appendToLI has no return statement

function appendToLI(elementName, property, value) {
   const element = createElement(elementName, property, value);
   li.appendChild(element);
  };

I'm wondering why there is no return statement in this function. Is it because li.appendChild(element); is already a working value?

function createElement (elementName, property, value) {
   const element = document.createElement(elementName);
   element[property] = value;
   return element;
  }

we must include "return element;" because otherwise there is no value?

2 Answers

Steven Parker
Steven Parker
231,269 Points

A "return" statement at the end of the function can be omitted when the function does not return any value, as is the case with "appendToLi".

On the other hand, "return" is necessary to make the function return a value.

Doron Geyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

Trevor Maltbie it may be easier for you to think of it in real world terms. A function without a return statement in pseudo code:

pick up this item and deliver it to the store 
// the function ends as soon as it has delivered the item there is nothing further it needs to do 

function with return statement in pseudo code

pick up this item and deliver it to the store
let me know when youve done it ( return task completed)
// in this case you want it to return a value to let you know the task has been completed.

its as simple as that either the function needs to complete a task in this case appendToLI has a task to do and doesnt need to provide you with further information to use elsewhere. Or it needs to complete a task and provide you with information to use elsewhere which is your "returned" bit.