JavaScript console.log()

Have you ever scratched your head trying to figure out why your JavaScript code isn't working? You've double-checked your variables, functions, and syntax, but you still can't find the problem. Fear not, my fellow developer: console.log() is a simple and powerful tool at your disposal.

This seemingly insignificant command can be the key to unlocking the mysteries of your code and transforming you into a JavaScript wizard. Let's start with its brief description.

The JavaScript console.log() method is used to write data or messages to the web console. For example:

<!DOCTYPE html>
<html>
<body>

   <script>
      console.log("Hello there!");
   </script>
   
</body>
</html>

To see the output, either press the F12 function key or navigate to Console. Or click on the three vertical dots (in Google Chrome) or three vertical lines (in Mozilla Firefox), available at the right top side of the browser window, as shown in the snapshot given below:

js console log

Then navigate to More tools -> Developer tools, as shown in the snapshot given below:

JavaScript console log

A new interface will be opened in the same window. Now click on the Console to see the output, as shown in the final snapshot given below:

console log example JavaScript

JavaScript console.log() Syntax

The syntax of the console.log() method in JavaScript is:

console.log(message)

The message parameter is required and is used to display the log message on the console.

Note: If you noticed, the console is available under the Developer tools section. That is why developers use console.log() as their tool for testing or debugging purposes.

The Console object, which is included with web browsers and Node.js environments, has a method called console.log(). This indicates that you don't need to set it up or configure it further before using it. Any data type, including strings, numbers, objects, arrays, and more, can be used as one or more of the arguments that it can accept. It is a versatile tool for debugging and troubleshooting your code as a result.

Your output can be formatted with placeholders and substitutions using the console.log() method, making your logs easier to read and more informative. It's critical to keep in mind that console.log() is a debugging tool, so any console.log() statements in your production code should be removed. Leaving console.log() statements in your code can cause your application to run more slowly in addition to cluttering the console output.

Advantages of the console.log() function in JavaScript

Disadvantages of the console.log() function in JavaScript

JavaScript Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!