Just like any other programming/scripting language, one of the most common ways to debug your code is by using print statements. In JavaScript, there are several ways to print messages to the console or to the page. In this article, we'll explore some of the most popular ways to print messages in JavaScript, including,
- console.log()
- console.error()
- document.write()
- alert()
- window.print()
console.log()

console.log('Hello, world!');
The console.log() method is the most commonly used way to print messages to the console in JavaScript. It takes one or more arguments and prints them to the console, separated by commas.
You can also pass multiple arguments to console.log()
Example:console.log('The answer is', 42);
console.log() method can be very useful for debugging your JavaScript code, you can also print the results of function calls to the console.
You can see the console logs using the Inspect Dev Tool of any web browser and under console tab.
console.error()
-
Exmple:
console.error('Error occurred while calculating Interest.');
The console.error() method is similar to console.log(), but it's used to print error messages to the console. Error messages should be printed using the console.error() making it easier to spot them in the console.
document.write()
-
Example:
document.write('Hello, world!');
The document.write() method should be used to print messages directly to the HTML page. It takes one argument, which is the message to be printed.
window.print()
-
Example:
window.print();
The window.print() method is used to print the current page. When called, it opens the print dialog box, allowing the user to print the page.
The above code will open the print dialog box, allowing the user to print the current page.
window.print() can be useful for allowing users to print out important information from your web page.
alert()

alert('Hello, world!');
The alert() method can be used to display a message box with a message and an OK or Close button on the web browser. It takes one argument, which is the message to be displayed.
Note: alert() can be at times useful for displaying important messages to the user, but it can also be annoying and have a bad user experience if overused.
Facing issues? Have Questions? Post them here! I am happy to answer!
- Get Device Screen Width and Height using javaScript
- How to Print from JavaScript HTML using window.print
- Send Extra Data with Ajax Get or Post Request
- How to get UTC (GMT) time using JavaScript
- How to detect Browser and Operating System Name and Version using JavaScript
- Fix: Error: error:0308010C:digital envelope routines::unsupported NodeJs/Vue/React
- How to send email from JavaScript HTML using mailto
- Remove items from JavaScript array
- JavaScript : Get current page address
- Add Animated Scrolling to Html Page Title Script
- [javaScript] Convert text case to lowercase
- JavaScript date in yyyy-MM-dd format
- Power of Print Statements in JavaScript: A Comprehensive Guide
- Submit html form on dropdown menu value selection or change using javascript
- Write javaScript code in Swedish using FikaScript
- Javascript convert text case from uppercase to lowercase
- Excel Fix: SECURITY RISK Microsoft has blocked macros from running because the source of this file is untrusted.
- JavaScript: Count Words in a String
- Meaning of javascript:void(0) explained with example
- JavaScript: Generate Random Numbers between 1 and 3
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- How to get current URL Location using Javascript HTML
- Fix: SyntaxError: The requested module does not provide an export named default
- Fix: Error: Cannot find module /node-examples/init
- JavaScript: Check if variable is a number
- Bash command to Read, Output and Manipulate JSON File - Bash
- Generate Project Dependency tree using Gradle Command - Gradle
- Bash Hello World! Script Tutorial - Bash
- GitPython: How to check out a Branch - Python
- Bash Command To Check If File Exists - Bash
- How to Vertically Center Align Text in a Div using CSS Code Example - CSS
- How to Split a String using delimiter in Python - Python
- Check macOS free disk space using Terminal command - MacOS