- Adds interactive/interesting behavior to the web pages like hiding information with the click of a button, color-changing buttons, zoom-in or zoom-out on an image, etc.
Method 1: Using innerHTML method
We can use the basic HTML to display a string or print a message in our web page.
<!DOCTYPE html> <html> <body> <h2>Print something using innerHTML method</h2> <p>Sum of 10 and 30</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = 30 + 10; </script> </body> </html>
Method 2: Using document.write() method:
<!DOCTYPE html> <html> <body> <h1>Print something using document.write() method </h1> <p>Sum of 10 and 20</p> <script> document.write(10 + 20); </script> </body> </html>
Method 3: Using windows.alert()
<!DOCTYPE html> <html> <body> <h1>Print something using window.alert() method</h1> <p>Sum of 5 and 6</p> <script> window.alert(5 + 6); </script> </body> </html>
Method 4: Using the console.log() method:
This method prints the output in the console. To go to the console section, right-click on your page or browser and select inspect, then click on console to see the output.
<!DOCTYPE html> <html> <body> <h2>Print something in console.log method</h2> <p>The product of 8 and 10 is</p> <script> console.log(8 * 10); </script> </body> </html>
To sum up: