Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
JavaScript Output
❮ Previous Next ❯
JavaScript Display Possibilities
JavaScript can "display" data in different ways:
Writing into an HTML element, using innerHTML .
Writing into the HTML output using [Link]() .
Writing into an alert box, using [Link]() .
Writing into the browser console, using [Link]() .
Using innerHTML
To access an HTML element, JavaScript can use the [Link](id) method.
The id attribute defines the HTML element. The innerHTML property defines the HTML
content:
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
Tutorials Exercises Services Sign Up Log in
<p id="demo"></p>
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
<script>
[Link]("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
Try it Yourself »
Changing the innerHTML property of an HTML element is a common way to display data in HTML.
Using [Link]()
For testing purposes, it is convenient to use [Link]() :
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
[Link](5 + 6);
</script>
</body>
</html>
Try it Yourself »
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Using [Link]() after an HTML document is loaded, will delete all existing HTML:
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<button type="button" onclick="[Link](5 + 6)">Try it</button>
</body>
</html>
Try it Yourself »
The [Link]() method should only be used for testing.
ADVERTISEMENT
Using [Link]()
You can use an alert box to display data:
Tutorials
Example Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
[Link](5 + 6);
</script>
</body>
</html>
Try it Yourself »
You can skip the window keyword.
In JavaScript, the window object is the global scope object. This means that variables, properties,
and methods by default belong to the window object. This also means that specifying the window
keyword is optional:
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
alert(5 + 6);
</script>
</body>
</html>
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Try it Yourself »
Using [Link]()
For debugging purposes, you can call the [Link]() method in the browser to display data.
You will learn more about debugging in a later chapter.
Example
<!DOCTYPE html>
<html>
<body>
<script>
[Link](5 + 6);
</script>
</body>
</html>
Try it Yourself »
JavaScript Print
JavaScript does not have any print object or print methods.
You cannot access output devices from JavaScript.
The only exception is that you can call the [Link]() method in the browser to print the
Tutorials
content of Exercises
window.
the current Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Example
<!DOCTYPE html>
<html>
<body>
<button onclick="[Link]()">Print this page</button>
</body>
</html>
Try it Yourself »
?
Exercise
What is NOT a correct syntax for writing output in JavaScript?
[Link]()
[Link]()
[Link]()
[Link]()
Submit Answer »
❮ Previous Next ❯