Language: EN

javascript-console

How to use console in JavaScript

The Console object is a tool that allows us to display information on the screen (for example, for debugging or analyzing applications).

It is part of the global JavaScript API and does not need to be imported or instantiated. We simply have it available through the global console object.

Depending on where we are executing our JavaScript file

  • In a browser, the output will be in the development tools (DevTools)
  • In a runtime like Node.js, the output will be in the terminal

In JavaScript the console is used continuously. So let’s look at some of its main methods and utilities 👇.

What is console.log

The method you will use most frequently is console.log. Its main purpose is to print information to the console.

console.log(object);

Where object can be any type of data you want to print

Let’s see it with a simple example. If we do,

let message = "Hello, LuisLlamas!";

console.log(message);

When executing the above code in a browser, Hello, LuisLlamas! will be printed to the console.

This is useful to verify that variables contain the expected values and that the execution flow is correct (giving us messages when a point is crossed).

console.log is not only used to display texts. It can handle a wide variety of data types, from primitive types to more complex structures.

Additional options for console.log

In addition to its basic use, the console.log method offers various options to enhance console output.

Multiple parameters

You can pass multiple values to the console.log method, and it will print them consecutively, separated by a space.

console.log(value1, value2, ..., valueN);

For example, it is possible to use it to include a label describing what you are showing:

console.log("Result of the operation:", result);
// Output: Result of the operation: <value of result>

Or to show several related variables or values in a single line:

const name = "Maria";
const age = 25;
const city = "Madrid";

console.log("Name:", name, "Age:", age, "City:", city);
// Output: Name: Maria Age: 25 City: Madrid

Formatting with placeholders

console.log supports the use of placeholders, allowing you to control the format of the output.

  • %s (for strings)
  • %d (for integers)
  • %o (for objects)
const name = "Luis";
const age = 30;

console.log("Hello, my name is %s and I am %d years old", name, age);
// Output: Hello, my name is Luis and I am 30 years old

Custom styles in the console

In environments like browsers, you can apply CSS styles to your messages using the %c placeholder to visually highlight messages.

console.log("%cHello, World!", "color: blue; font-size: 20px; font-weight: bold;");
// Output: The text will appear styled in blue, with a large size and bold.

You can use multiple styled declarations in a single message:

console.log(
  "%cSuccess:%c The operation completed successfully.",
  "color: green; font-weight: bold;",
  "color: black;"
);
// Output: Success: appears in green and bold; the rest in normal black.

Styles are supported in most modern browsers, but are not available in Node.js.

Additional console methods

In addition to console.log, the console object offers several additional methods that can be useful for different types of debugging:

Practical examples of using console.log

Additional considerations

  • Never in production: Limit the use of console to the development stage. Make sure to remove debugging messages before production.
  • Security and privacy: Never use console to print sensitive information, such as passwords or personal data.