how to print javascript variable value in html page?

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>

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.