4 different Ways to Get JavaScript Output
July 26, 2019

JavaScript gives us 4 different options to print the output.

  1. innerHTML will write output in HTML element
  2. document.write() will write into HTML output
  3. window.alert() will show output in alert box
  4. console.log() will write output in browser console.

Let’s look at those 4 ways one by one.

1. innerHTML

This is the most common method of displaying data (output).

To access the HTML element we use document.getElementById (name of the id in html) method. We assign ‘id’ to HTML element and innerHTML is the content of that element.

Take a look at the example below.

<body data-rsssl=1><h1>.innerHTML Example</h1><p id=example1></p> <script>document.getElementById("example1").innerHTML="innerHTML example";</script> <script>window.w3tc_lazyload=1,window.lazyLoadOptions={elements_selector:".lazy",callback_loaded:function(t){var e;try{e=new CustomEvent("w3tc_lazyload_loaded",{detail:{e:t}})}catch(a){(e=document.createEvent("CustomEvent")).initCustomEvent("w3tc_lazyload_loaded",!1,!1,{e:t})}window.dispatchEvent(e)}}</script><script async src=https://www.yogeshchauhan.com/wp-content/cache/minify/1615d.js?x75580></script> </body> /* Outptut */ innerHTML example

2. document.write()

<script>document.write("document.write Example");</script>

The problem with document.write is that if you call it after the document is loaded, then it’ll delete all the existing HTML code and write the new one with the new output.

3. window.alert()

We can use alert box to display output but it’s not useful in all situations. It’s really useful for warnings only. For e.g. if user enters text instead of number. Take a look at the example below…

<body data-rsssl=1> <button onclick=example3()>Click to get the alert</button> <script>function example3(){window.alert("You got me!");}</script> </body>

It will give you an alert when you click on the button.

4. console.log()

We use console.log() mostly for debugging purposes only. You really can’t see the output on your browser screen. Take a look…

<script>console.log("I am console.log Example");</script>
In Google Chrome, you have to right click on the page and then click on ‘inspect’ and then go to ‘console’ to see the output.


Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 5 How to create flickering text using CSS? Mar 4 How to use data-* Attributes in HTML? Mar 4 The substr() method in JavaScript and how it’s different from substring() Mar 4 A complete guide to add responsive YouTube videos using HTML and CSS Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress?

You might also like these

How to Create a Copy of a Table in SQL and MySQL?SQL/MySQLIntroduction to components and templates Part 3: Data bindingAngularContainers 101: What are containers?MiscHow to create a pricing table using flex in CSS?CSSHow to hide a DIV on clicks outside of it using jQuery?jQueryIf statement shorthand examples in JavaScriptJavaScript