JavaScript screen output codes
JavaScript does not have any built-in print and display functions. Javascript has possibilities to write to the screen in different ways (window.alert (), document.write (), innerHTML, console.log ()).
- window.alert () : Write to the screen using window.alert()
- document.write () : Write to HTML output using document.write()
- innerHTML: Writing into an HTML element using innerHTML
- console.log () : Write into the browser console.
Using Window.alert()
You can use an alert box to view the data:
window.alert("Hello World");
USING document.write()
document.write(): For testing purposes, suitable for use.
After the HTML document is fully loaded, using document.write(), all existing HTML is deleted and the message is written.
document.write("Hello World");
NOTE: The document.write() method deletes all HTML tags and writes a message to the screen. It is used for testing only.
USING InnerHTML
With this method, a message can be written by selecting any HTML element.
JavaScript:
document.getElementById("demo").innerHTML = 5 + 6;
HTML:
<p id="demo"></p>
Using console.log
You can use console.log() to view data in your browser. Activate the browser console with F12.
JavaScript:
console.log("hello world");
Webmaster can use it for testing purposes. Other console features:
- console.info(): The Web Console displays an informational message. In Firefox and Chrome, a small "i" icon is displayed next to these items in the Web Console log.
- console.error() : The Web Console returns an error message.
- console.warn() : Gives a warning message for the Web Console. Note: Firefox has a small exclamation point in the Web Console log next to the warnings.
Let's make an example with the console.log function.
JavaScript:
console.info("Information");
console.log("hello world");
console.warn("Attention wrong operation");
console.error("Attention error");
When we run our browser, we cannot see the screen outputs in the example above. To see the output, we can see it by running the console of our browser with the F12 keyboard key.