JavaScript Output
JavaScript Display Possibilities
JavaScript can show data in various ways.:
- Writing into an HTML element, using
innerHTML
. - Writing into the HTML output using
document.write()
. - Writing into an alert box, using
window.alert()
. - Writing into the browser console, using
console.log()
.
Using innerHTML
To get an HTML part, JavaScript can use the document.getElementById(id)
method.
The id
attribute tells you which part of a webpage something is. The innerHTML
property tells you what's inside that part.
Modifying what's inside an HTML element is a usual method to show information on a webpage.
Using document.write()
For testing, it's convenient to use document.write()
.
If you use document.write() after a web page has already loaded, it will erase all the content that was already there.
You should only use the document.write() method for testing purposes.
Using window.alert()
You can use an alert box to show information.
You have the option to leave out the window
keyword.
In JavaScript, the window object is like the big boss. It's where things like variables, properties, and methods live by default.
So, when you work with JavaScript, you don't always have to say window
explicitly.
Using console.log()
To find and fix problems in your code, you can use the console.log()
method in your web browser. This method helps you show information on your browser's console.
In an upcoming section, we will delve deeper into the topic of debugging.
JavaScript Print
In JavaScript, there are no specific objects or methods designed solely for printing.
In JavaScript, you're unable to interact with output devices.
The only special case is when you want to use the window.print()
command in a web browser to print what's currently on the screen.