Ways to Format Console Output in JavaScript

Last Updated: Feb 10, 2024 by

When working with JavaScript, it is important to be able to effectively display information in the console. This can help with debugging and understanding the flow of your code. In this article, we will discuss some ways to format console output in JavaScript.

Using console.log()

The most basic way to display information in the console is by using the console.log() method. This method takes in any number of arguments and displays them in the console. For example:

console.log(“Hello”, “World”);

This will output Hello World in the console. However, sometimes we may want to format the output in a specific way.

String Concatenation

One way to format console output is by using string concatenation. This involves combining multiple strings together using the + operator. For example:

console.log(“Hello” + ” ” + “World”);

This will output Hello World in the console. While this method works, it can become tedious and difficult to read when dealing with longer strings or multiple variables.

Template Literals

Template literals are a newer feature in JavaScript that allow for more flexible string formatting. They use backticks (`) instead of single or double quotes and allow for the use of placeholders and expressions within the string. For example:

let name = “John”; console.log(`Hello ${name}`);

This will output Hello John in the console. Template literals also allow for multi-line strings, making it easier to format console output with line breaks.

Formatting with CSS

Another way to format console output is by using CSS. This can be useful when wanting to add color or styling to the output. To do this, we can use the %c placeholder in our console.log() statement and pass in a CSS style as the second argument. For example:

console.log(“%cHello World”, “color: blue; font-size: 20px;”);

This will output Hello World in blue and with a font size of 20px. This method can be especially useful when wanting to highlight certain information or errors in the console.

Using console.table()

The console.table() method allows for displaying data in a tabular format in the console. This can be useful when working with arrays or objects. For example:

let fruits = [“apple”, “banana”, “orange”]; console.table(fruits);

This will output a table in the console with the values of the fruits array. This method can also be used with objects, displaying the key-value pairs in a table format.

Placeholder Substitution

Placeholder substitution is another way to format console output in JavaScript. This involves using placeholders in a string and passing in the values as arguments to the console.log() method. For example:

let name = “John”; console.log(“Hello %s”, name);

This will output Hello John in the console. Placeholder substitution can also be used with multiple values and different types of placeholders, such as %d for numbers and %o for objects.

By using these techniques, you can effectively format console output in JavaScript and make your debugging and development process smoother. Experiment with these methods and find what works best for you and your code.

Have you tried any of these methods before? Do you have any other tips for formatting console output in JavaScript? Let us know in the comments.

Gulrukh Ch

About the Author: Gulrukh Ch

Gulrukh Chaudhary, an accomplished digital marketer and technology writer with a passion for exploring the frontiers of innovation. Armed with a Master's degree in Information Technology, Gulrukh seamlessly blends her technical prowess with her creative flair, resulting in captivating insights into the world of emerging technologies. Discover more about her on her LinkedIn profile.