Practice: Using JavaScript and the DOM to Output Data to a Webpage

In this exercise, we'll use JavaScript to output data to a webpage using the write method on a document object. First, read this article that describes document.write(). The article also describes two other methods we've used in this course: console.log() and alert(). This exercise does not count towards your grade. It is just for practice!

The following JavaScript program shows how to combine all of the elements described in the last 3 sections. 

<html>
 <head>
      <title>First JavaScript Program </title>
 </head>
 <body>
   <script>
     /*
       This is an example of JavaScript
     */
     console.log("The program is running") // Write to the console
     document.write("<h1>First JavaScript Program</h1>") //Print head
     document.write("This is my first web page with JavaScript.");
     document.write("<br/>This is on a new line");
     alert("Here is an alert box");
    </script>
  </body>
</html>

Program 24 First JavaScript Program

The result of running this html is the following web page.


Figure 3 – Final Web Page