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 Useful Array Methods

Two questions, one about print() and one about else/else if

  1. How does print work? If we ever went over that, I missed it. What is the difference between print and document.write? Are they both methods?

  2. What is the difference between:

else {
  if () {

  }
}

As shown in the video, and:

else if () {

}

Like we were taught?

1 Answer

Steven Parker
Steven Parker
231,275 Points

The "print" function was introduced at about 3:45 in the Useful Array Methods video. It is not one of the array methods, it was just provided as a handy way to display some text on the web page. As it is, there's no significant difference between it and "document.write", since all it does is call the other one internally. But that could be changed later.

If no code is added except within the inner block, there is no functional difference between the two examples you have shown above. The potential advantage of the first form is when you want to do something in the "else" case in addition to what is in the "if". And the advantage of the second form is that you may add more "else if" cases after the block and/or a final "else" clause.

Thank you! Sounds like I'm asking questions that don't need to be asked this early in my training.

Steven Parker
Steven Parker
231,275 Points

No doubt many questions will be answered in later videos, but most of the folks who answer questions here can appreciate an relate to an "enthusiastic learner". :wink:

Happy coding!