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
Comments
hotnewtop
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.
Example:

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 Repl.it terminal, though. Only in the browser terminal)
https://storage.googleapis.com/replit/images/1557092960512_e49e6239356ceb909f628b36d9d9b7db.pn

Steven_The_GuyT (329)

@mat1 Yeah, I don't think it works

mat1 (4427)

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

eankeen (2176)

https://storage.googleapis.com/replit/images/1557090678120_1e113fe3651ba89f428d97dc41edfabe.pn
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

However
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
Repl.it supports colors in the terminal.
You can use my repl.it as example for usage:
https://repl.it/@TalesK/chalk-test

Thanks to @eankeen for usage demo!