-1

I'm creating a game played in the console and I would like to decorate my console.log messages to give them more flare.

Since I'm new to coding, I'm trying to learn to use template literals any time I console.log a message.

If I reverted to quotes and concatenation, I could style my console.log messages like so: console.log('%c some message' + someVar, 'color: red')

How can I style my console.log messages using template literals?

j08691
  • 190,436
  • 28
  • 232
  • 252

2 Answers2

0

You can use a template literal for the first argument to console.log instead of concatenation.

console.log(`%c some message ${someVar}`, 'color: red')

You can also use dynamic styles by using a template literal for the second argument as well.

iota
  • 34,586
  • 7
  • 32
  • 51
0

You can use this type of formatting.

console.log(`%c ${message} ${someVar}`, `color:${color}`)

Works like a charm.

Hultan
  • 995
  • 1
  • 8
  • 17