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 trialJimmy Morgan
18,237 PointsWhat 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
231,110 PointsI 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.