Colors in JavaScript console

Colors in JavaScript console
Colors in JavaScript console

In JavaScript, the console object provides a number of methods that can be used to log information to the browser’s developer console. Some of these methods, such as console.log(), console.info(), and console.error(), allow you to output text to the console. By default, the text that is logged to the console is displayed in black.

You can also change the color of the text that is logged to the console by using special control characters in the string that you pass to the console.log() method. For example, the following code logs the string “Hello, world!” to the console in red:

console.log("%cHello, world!", "color: red;");

The %c is special formatting character which triggers the second argument in console.log method which can have css property

Here are some examples:

console.log("%cHello, World", "color:blue;font-size:20px;") 
console.log("%cHello, World", "color:green;background-color:black;")

It’s also worth noting that different browser may or may not support this feature. But this is widely supported feature.

Leave a Reply