JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

Jesse Vorvick
Jesse Vorvick
Full Stack JavaScript Techdegree Student 4,459 Points

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
173,537 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.

Jesse Vorvick
Jesse Vorvick
Full Stack JavaScript Techdegree Student 4,459 Points

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

Steven Parker
Steven Parker
173,537 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!