JavaScript Program to Write to Console
Console statements are a very useful in Javascript. They let you log information to the console, and they can be used for a variety of purposes.
The most common use case is when you want to trace or debug your code. This is especially useful when you're writing front-end code, as it can help you figure out what's going on with your site.
Another use case is when you want to check if something has been set correctly by other code, such as if a variable has been set to a certain value.
Yet another use case for console statements is for error handling. You can use them to display errors that have occurred in your program, and then let you take action based on those errors.
The console object provides us with several different methods.
Console methods
- Using console.log()
- Using console.error()
- Using console.warn()
- Using console.clear()
In the following sections, we will learn how to use the above methods to log information to the console.
1. Using console.log()
Console.log() is a method that logs a message to the console. It usually takes a single argument, which is the message that you want to log. Sometimes, it can take multiple arguments separated by commas.
// print a string to the console
console.log('Hello World'); // prints 'Hello World' to the console
// print a number to the console
console.log(123); // prints 123 to the console
// print a boolean to the console
console.log(true); // prints true to the console
// print an object to the console
console.log({ name: 'Jane', age: 25 }); // prints { name: 'Jane', age: 25 } to the console
// print an array to the console
console.log([1, 2, 3, 4, 5]); // prints [1, 2, 3, 4, 5] to the console
// print a function to the console
console.log(function () {
return 'Hello World';
}); // prints function() { return 'Hello World' } to the console
// print a null value to the console
console.log(null); // prints null to the console
// print a undefined value to the console
console.log(undefined); // prints undefined to the console
// print a NaN value to the console
console.log(NaN); // prints NaN to the console
// print a symbol to the console
console.log(Symbol('foo')); // prints Symbol(foo) to the console
When do we use console.log()?
- When we want to log information to the console.
- When we want to debug our code.
- When we want to check if something has been set correctly by other code.
- When we want to display errors that have occurred in our program.
2. Using console.error()
Console.error() is a method that logs an error message to the console. It usually takes a single argument, which is the message that you want to log. This prints the message in red, so that it can be distinguished from other messages.
// print an error message to the console
console.error('This is an error message'); // prints 'This is an error message' to the console
// print an error message to the console with a custom error object
console.error('This is an error message', { name: 'Jane', age: 25 }); // prints 'This is an error message' to the console with { name: 'Jane', age: 25 }
// print an error message to the console with a custom error object and a custom error message
console.error(
'This is an error message',
{ name: 'Jane', age: 25 },
'This is a custom error message'
); // prints 'This is an error message' to the console with { name: 'Jane', age: 25 } and 'This is a custom error message'
When do we use console.error()?
- When we want to log an error message to the console
- When we want to log an error message to the console with a custom error object
- When we want to log an error message to the console with a custom error object and a custom error message
3. Using console.warn()
Console.warn() is a method that logs a warning message to the console. It usually takes a single argument, which is the message that you want to log.
// print a warning message to the console
console.warn('This is a warning message'); // prints 'This is a warning message' to the console
// print a warning message to the console with a custom warning object
console.warn('This is a warning message', { name: 'Jane', age: 25 }); // prints 'This is a warning message' to the console with { name: 'Jane', age: 25 }
// print a warning message to the console with a custom warning object and a custom warning message
console.warn(
'This is a warning message',
{ name: 'Jane', age: 25 },
'This is a custom warning message'
); // prints 'This is a warning message' to the console with { name: 'Jane', age: 25 } and 'This is a custom warning message'
When do we use console.warn()?
Console.warn() is usually used when we want to warn the user of a potential problem, but we don't want to stop the execution of the program.
We use it
- When we want to log a warning message to the console
- When we want to log a warning message to the console with a custom warning object
- When we want to log a warning message to the console with a custom warning object and a custom warning message
4. Using console.clear()
Console.clear() is a method that clears the console.
// clear the console
console.clear(); // clears the console
When we use console.clear()?
- When we want to clear the console
Summary
In this tutorial, we have learnt how to use the console object to log information to the console. We have also learnt how to use the console object to log errors and warnings to the console. We looked into how we can clear the console using a simple console statement and we have also learnt to add custom objects and messages to the console.
Please checkout our related content section below for more tutorials on similar topics.
Related Content
- Learn more about JavaScript
- Learn more about JavaScript Arrays
- JavaScript Program to Add Two Numbers
- JavaScript Program to Calculate the Area of a Triangle
- JavaScript Program to Convert Date to a Number
- JavaScript Program to Find the Square Root of a Number
- JavaScript Program to Pass a Function as a Parameter
- JavaScript Program to Remove all Whitespaces From a Text
- JavaScript Program to Write Console Statements
- JavaScript Hello World Program
We hope that you have learnt something from our tutorials. Please feel free to share this content with your friends.
If you have any questions or want some other content to be added to our website so that it can help you learn and grow further, please do not hesitate to contact us at info@createmysite.org. We would love to hear from you.
We are hiring!! If you are tech blogger who loves writing content on modern technologies, we would love to get in touch with you. Just drop us a mail at the email address mentioned above and we will reach out to you as soon as possible.
If you are interested in learning more about our website, please visit our about page.