Today, We want to share with you how to print in javascript.In this post we will show you console.log javascript, hear for JavaScript: Print the contents of the current window we will give you demo and example for implement.In this post, we will learn about javascript console write with an example.
How to print to console using javascript?
- Using InnerHTML
- Using a Window Alert
- Using document.write()
- Using console.log()
- JavaScript Print
console.log(text);
Example 1:
<!DOCTYPE html> <html> <body> <h2>javascript output</h2> <p>how to display javascript variable value in html page?</p> <p id="output"></p> <script> document.getElementById("output").innerHTML = 9 + 22; </script> </body> </html>
Using InnerHTML
index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaScript innerHTML - www.pakainfo.com</title> </head> <body> <ul id="menu"> <li>Home</li> <li>Services</li> </ul> <script> let menu = document.getElementById('menu'); // create new li element let li = document.createElement('li'); li.textContent = 'About Us'; // add it to the ul element menu.appendChild(li); console.log(menu.innerHTML); </script> </body> </html>
Using a Window Alert
index.html
<!DOCTYPE html> <html> <body> <h2>Welcome to the JavaScriptTutorial.net website!</h2> <p>Pakainfo.com is a good place to start.</p> <script> window.alert(9 + 10); </script> </body> </html>
Using Console Log
index.html
<!DOCTYPE html> <html> <body> <script> console.log(8 + 7); </script> </body> </html>
Using document.write()
index.html
<!DOCTYPE html> <html> <body> <h2>Welcome to the JavaScriptTutorial.net website!</h2> <p>Pakainfo.com is a good place to start.</p> <script> document.write(4 + 3); </script> </body> </html>
Printing from a Printer
index.html
<!DOCTYPE html> <html> <body> <h2>Welcome to the JavaScriptTutorial.net website!</h2> <p>Pakainfo.com is a good place to start.</p> <button onclick="window.print()">Print this page</button> </body> </html>
how to print variable in javascript?
To print a variable value in JavaScript, you can use the console.log() function. This function writes a message to the console log, which can be viewed in the browser’s developer console. Here’s how you can use console.log() to print a variable value in JavaScript:
// Declare a variable var myVariable = 'Hello, world!'; // Print the variable value to the console log console.log(myVariable);
In the above code, we declare a variable called myVariable and set its value to the string ‘Hello, world!’. We then use console.log() to print the value of the variable to the console log. When you run this code in a web browser and open the developer console, you will see the message ‘Hello, world!’ printed to the console log.
I hope you get an idea about javascript print function.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.