Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,136 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
216,136 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!