JavaScript Output
JavaScript Basics

JavaScript Output

JavaScript Output


JavaScript Display Possibilities

JavaScript can display data in several ways:

  • Writing into an HTML element, using innerHTML.
  • Writing into the HTML output using document.write().
  • Displaying data in an alert box, using window.alert().
  • Logging data to the browser console, using console.log().


Using innerHTML

To manipulate an HTML element, JavaScript can utilize the document.getElementById (id) method.
The 'id' attribute specifies which HTML element to access, and the 'innerHTML' property allows you to set or modify the HTML content within that element:

Example

<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>
<p>My First Paragraph.</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>

</body>
</html> 

Modifying the 'innerHTML' property of an HTML element is a frequently used method for displaying data within HTML.

Using document.write()

For testing purposes, using 'document.write()' is a convenient way to output data directly to an HTML document.

<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>
<p>My first paragraph.</p>

<p>Never call document.write after the document has finished loading.
It will overwrite the whole document.</p>

<script>
document.write(5 + 6);
</script>

</body>
</html> 


Using document.write() after an HTML document is loaded, will delete all existing HTML:

Example

<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>
<p>My first paragraph.</p>

<button type="button" onclick="document.write(5 + 6)">Try it</button>

</body>
</html> 

The document.write() method should only be used for testing.

Using window.alert()

You can use an alert box to display data:

<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>
<p>My first paragraph.</p>

<script>
window.alert(5 + 6);
</script>

</body>
</html> 

In JavaScript, the window object represents the global scope, which means that variables, properties, and methods by default are part of the window object. Therefore, it's optional to explicitly use the window keyword when referencing these elements.

Example

<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>
<p>My first paragraph.</p>

<script>
alert(5 + 6);
</script>

</body>
</html> 

Using console.log ()

For debugging purposes, you can use the console.log() method in the browser to display data. More detailed techniques and strategies for debugging will be covered in a later chapter.

Example


<!DOCTYPE html>
<html>
<body>

<h2>Activate Debugging</h2>

<p>F12 on your keyboard will activate debugging.</p>
<p>Then select "Console" in the debugger menu.</p>
<p>Then click Run again.</p>

<script>
console.log(5 + 6);
</script>

</body>
</html> 

JavaScript Print

JavaScript does not possess any built-in print objects or methods to directly access output devices.

The sole exception is the ability to use the method in the browser, which allows you to print the content of the current window.


Example

<!DOCTYPE html>
<html>
<body>

<h2>The window.print() Method</h2>

<p>Click the button to print the current page.</p>

<button onclick="window.print()">Print this page</button>

</body>
</html>
Take a look into your desired course