Different possibilities to get the output of javascript code. These are
We can use the javascript tag document.getElementById('yourID') method to access an HTML element using JavaScript. The attribute id defines in the HTML tag where as innerHTML
property defines the HTML content.
Try this Example Now
<!DOCTYPE html> <body> <p id="myoutput"></p> <script> document.getElementById("myoutput").innerHTML = 10 + 2; </script> </body> </html>
This method document.write() is only used for testing purposes. We can use it directly to onclick event in HTML or use in the script tag.
Example: TRY THIS EXAMPLE NOW
Try this Example Now
<!DOCTYPE html> <html> <body> <button type="button" onclick="document.write(10 + 8)">Click to see output</button> </body> </html>
Try this Example Now
<!DOCTYPE html> <html> <body> <script> document.write(5 + 6); </script> </body> </html>
You can use an alert box to display data of your javascript code. You can skip the window
keyword. This means window
keyword is optional while writing a javascript code.
Try this Example Now
<!DOCTYPE html> <html> <body> <script> window.alert(10 + 2); alert(10 + 2); </script> </body> </html>
You can call the console.log()
method in the browser to display data. it is only use for debugging the javascript code.
Try this Example Now
<!DOCTYPE html> <html> <body> <script> console.log(5 + 6); </script> </body> </html>