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.
Have Questions? Post them here!
- How to get UTC (GMT) time using JavaScript
- Submit html form on dropdown menu value selection or change using javascript
- How to detect Browser and Operating System Name and Version using JavaScript
- JavaScript : Get current page address
- [javaScript] Convert text case to lowercase
- How to check if a String contains substring or a word using javaScript
- Writing your first Hello, World! 🌍 JavaScript code Tutorial
- JavaScript: Check if variable is a number
- How to send email from JavaScript HTML using mailto
- Meaning of javascript:void(0) explained with example
- How to get query string in JavaScript HTML location.search
- Javascript convert text case from uppercase to lowercase
- Loading previous page using html button using JavaScript
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- How to get current URL Location using Javascript HTML
- Detect if Cookies are enabled using JavaScript
- Write javaScript code in Swedish using FikaScript
- Add Animated Scrolling to Html Page Title Script
- JavaScript: Convert an Image into Base64 String
- Remove items from JavaScript array
- Send Extra Data with Ajax Get or Post Request
- How to Print from JavaScript HTML using window.print
- Get Device Screen Width and Height using javaScript
- Examples: Convert String to int in JavaScript
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- Create S3 bucket using AWS CLI Command mb - AWS
- Binary, Decimal, Octal, and Hexadecimal Numbers Tables - 2022
- 3 Python program to add two numbers - Python
- Where does brew install python in macOS - Python
- Remove duplicate lines using Notepad++ - NotepadPlusPlus
- How to get the SharePoint Tenant Login URL - SharePoint
- Add comma or semicolon at end of each line Notepad++ - NotepadPlusPlus
- How to create SharePoint List Item programmatically using C#.net - SharePoint