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 Data Using Objects The Build an Object Challenge, Part 2 Solution

What is the prefered syntax, and is there a reason the instructor chooses this method?

The lesson instructs us to use the print function as such.

function print(message) { var outputDiv = document.getElementById('output'); outputDiv.innerHTML = message; }

The method i was taught takes less space and seems easier to read.

function print(message) { document.getElementById('output').innerHTML = message; }

The result is the same. Is there a reason the instructor prefers the first method?

1 Answer

Steven Parker
Steven Parker
231,248 Points

I can't speak for the instructor, but based on many questions I've seen in the forum, students often find concepts easier to understand when broken down into steps. This is probably the value in performing the element selection separately from the attribute assignment.

In actual practice, I would also write it as a one-liner as in your second example, unless the reference to the element was to be used again later in the function.