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

Kieran Barker
Kieran Barker
15,028 Points

In JavaScript programming, what is the benefit of putting the document.write() method inside a function?

What is the benefit of doing this...

function print(message) {
  document.write(message);
}

Instead of just this?

document.write( // whatever I want to print out );

Also, am I right in calling document.write() a method? Methods and functions look the same to me!

1 Answer

Steven Parker
Steven Parker
231,545 Points

I can think of two benefits:

  • it makes the code more compact, particularly if it is used multiple times
  • it makes it easy to change how the output is done later at just one point in the code