Basic JavaScript: Exercise 02 : Output to HTML

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.

Advertisements

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.

Advertisements
<html>

<head>
    <title>Second JavaScript</title>
</head>
<body>
    <script type="text/JavaScript">
    document.write ("<h1>This is my second JavaScript program !</h1>");
    </script>
</body>
</html>

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.

JavaScript Output in HTML
JavaScript Output in HTML

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.