X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript Output


JavaScript does not have any built-in display functions.


JavaScript Display Possibilities

JavaScript can "display" data in different ways:

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

Using document.write()

For testing purposes, it is convenient to use document.write():

Example

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>

<p>My first paragraph.</p>

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

</body>
</html>

Try it Yourself »

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

Example

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>

<p>My first paragraph.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    document.write(5 + 6);
}
</script>

</body>
</html>

Try it Yourself »

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


Using innerHTML

To access an HTML element, JavaScript can use the document.getElementById(id)) method.

The id attribute identifies the HTML element. The innerHTML property is the element content:

Example

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>

<p id="demo">My First Paragraph</p>

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

</body>
</html>

Try it Yourself »

Note In this tutorial, we will use the method described above (writing into an element with id="demo").


Using window.alert()

You can also use an alert box to display JavaScript values.

Example

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>

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

</body>
</html>

Try it Yourself »


Using console.log()

If your browser supports debugging, you can use the console.log() method to display JavaScript values.

Activate debugging in your browser with F12, and select "Console" in the debugger menu.

Example

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>

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

</body>
</html>

Try it Yourself »


Did You Know?

Note Debugging is the process of testing, finding, and reducing bugs (errors) in computer programs.
The first known computer bug was a real bug (an insect), stuck in the electronics.



Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook