JavaScript Code


How to print to Browser Console using JavaScript?


Tutorials using JS

Print to Browser Console

to print to browser console using JavaScript, we can use console.log() method

pass the string, as argument to console.log() method, that has to be printed to the console

console.log('Hello World');

to view browser’s console, open Developer tools in the browser, and switch to Console tab

Example

in the following HTML document, we have script that prints 'Hello World!' to console when user clicks on the Click Me button

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Tutorial</h1>
<h2>Print to console</h2>
<button id="myBtn">Click Me</button>

<script>
//set onclick listener for #myBtn
document.getElementById("myBtn").addEventListener("click", function() {
    //print to console
    console.log('Hello World!');
});
</script>

</body>
</html>

Copyright @2022