Ask coding questions

← Back to all posts
JS console colors
neodymium (33)

Just wondering if there is a way to change the text color using console.log in the Javascript console.

Answered by mat1 (4427) [earned 5 cycles]
View Answer
mat1 (4427)

You can change the color of a console.log by adding %c to the beginning of your message and adding css as the second argument.

console.log('%c Hello world ', 'background: #000; color: red');

Please mark this comment as the answer if it helped. Thanks!

Pythonier (316)

@mat1 I'm not sure that works.

mat1 (4427)

@Pythonier It works, try it. (Doesn't work on the terminal, though. Only in the browser terminal)

Steven_The_GuyT (329)

@mat1 Yeah, I don't think it works

mat1 (4427)

@Pythonier Doesn't work on the terminal, though. Only in the browser terminal
Press f12 and select console

eankeen (2176)
One of the most popular libraries to add colored text is called chalk.
However, it doesn't seem to be supported on replit at the moment. (see screenshot) :P

There is a way to do it.
Checkout this StackOverflow post. Basically, you can write something like this:

console.log('\x1b[31m%s\x1b[0m', 'I am red');

And have the output be red :)

I made a repl here if you want to check it out.

xdcoledabeast (0)

how do you put in JS text or print I'm new to coding JS plz help

TalesK (0)

Its WORKS! :D supports colors in the terminal.
You can use my as example for usage:

Thanks to @eankeen for usage demo!