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 Basics (Retired) Making Decisions with Conditional Statements Introducing Conditional Statements

Why are HTML tags used when a sentence is outputted in JavaScript?

Treehouse is normally really good at providing exceptional details regarding a programming language's syntax. However, there was never any mention as to why we're supposed to enclose a sentence in HTML tags (e.g. <h1></h1> or <p></p>) when using the document.write(), alert(), and other such methods in JavaScript.

This isn't something that was just introduced in this video but I thought I would ask now since it's continually used without any explanation.

Daniel Crews
Daniel Crews
14,008 Points

An example would help. The placement of HTML tags in a string that is being added to the DOM is not required, in general, and is dependent upon what you want your final markup to look like.

Here's a code snippet from the 'quiz.js' file in one of the lessons on conditionals:

var answer = prompt("What programming language is the name of a gem?");
if ( answer.toUpperCase() === 'RUBY' ) {
  document.write("<p>That's right!</p>");
} else {
  document.write("<p>Sorry, that's wrong.</p>"); 
}

(Note: I take back what I wrote previously regarding 'alert()' and other methods. The HTML tags seem to only be used with the 'document.write()' method.

5 Answers

Corey Smith
Corey Smith
3,520 Points

I'm late, but I had this question also. From what I gather, giving it the html tag makes the output inherit the CSS properties of that tag. For example, if you didn't include the <p> tag then the output would take on the default styling. If you include the <p> tag, then it will inherit the CSS properties for that tag, and hence it would look better and have the same styles as the rest of the page.

The JavaScript output is going to the browser so the HTML tags are rendered.

Daniel Crews
Daniel Crews
14,008 Points

HTML tags are added as needed, but not required. If you just wanted to add text to the DOM you could use:

document.write("Add this sentence");

If you wanted to have the sentence wrapped in paragraph tags you would use:

document.write("<p>Add this sentence in a paragraph</p>");

The paragraph tags could be any other HTML tag depending upon how you would like to mark up your content.

Pretty much, for example in html you dont actually need tags to display text, its raw. JavaScript is similar, it has the ability to use the html tags.

jennyhan
jennyhan
36,121 Points

It's similar in other languages. Like in PHP, it should be: echo "<p>Add this sentence in a paragraph</p>";