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 Multiple Items with Arrays Adding Data to Arrays

What is the use of print(html) ? From where did the variable "html" came from ?

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

what is the use of this function ? And from where did the "html" came from ?

1 Answer

print() is a function which has one parameter, html. The print function will write something to the document. When you call the function you will pass an argument to it, which will be the actual value you want to print to the document.

for example

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

const message = "<h1>!drloW olleH</h1>";

print(message);

In the video the print function is used to print out the list

function printList( list ) {
  var listHTML = '<ol>';
  for (var i = 0; i < list.length; i += 1) {
    listHTML += '<li>' + list[i] + '</li>';
  }
  listHTML += '</ol>';
  print(listHTML); //the listHTML is written to the document with the print function. The print function is declared below.

}

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

In javaScript, functions can be declared anywhere in the javascript document (taken that the function and the calling of the function are in the same execution context and have been created using function declaration and not function expression) and they still work even if they are called before they are declared