Basic JavaScript: Exercise 02 : Output to HTML

Previous Article
Next Article

In JavaScript exercise 1, you printed the output directly from the JavaScript program. There was no formatting. This is the way JavaScript program works, but HTML documents are meant to present information in a human-readable format with some style information.

Imagine reading plain notepad like web pages, there is no fun in that. A JavaScript output is then embedded within HTML tags so that output is in your desired format. In this post, the program from exercise is modified to present the output in HTML format.

JavaScript Program for HTML Output

To understand this script, you need some knowledge of HTML tags and their behavior. If you do not, then do not worry about it, just keep reading and try this program yourself.

<title>Second Javascript</title>
<script type="text/javascript">
document.write ("<h1>This is my second Javascript program !</h1>");

HTML document have 6 types of header – h1, h2, h3, h4, h5 and h6 . Any text within these tags will be shown in bold. HTML and JavaScript display a high-quality web page in this way.

Output in Browser

The output is shown in bold because it is a tag h1 element now. HTML tags are used to format the text elements.

Output: JavaScript Example Program for HTML
Output: JavaScript Example Program for HTML
Previous Article
Next Article