-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
  • 204,283
  • 31
  • 260
  • 272

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.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

You can use this type of formatting.

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

Works like a charm.

Hultan
  • 1,409
  • 1
  • 15
  • 28