JavaScript Output
In JavaScript, you can display data in different ways using innerHTML, document.write(), window.alert() & console.log().
Way to display data using JavaScript
JavaScript can display data in different ways:
- Using innerHTML
- Using document.write()
- Using window.alert()
- Using console.log()
Using innerHTML
To access an HTML element, JavaScript can use the document.getElementById(id) method.
The id attribute defines the HTML element. The innerHTML property defines the HTML content.
Example :
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = 'Hello World'; </script> </body> </html>
Using document.write()
You can use document.write() for testing purposes.
Example :
<!DOCTYPE html> <html> <body> <script> document.write('Hello World'); </script> </body> </html>
Using console.log()
Using console.log() you can see the data in browser console. This method is used for mostly debugging purposes.
Example :
<!DOCTYPE html> <html> <body> <script> console.log('Hello World'); </script> </body> </html>