chrome's console.log string substitution seems out of control

Viewed 48

I'm trying to show a string using console.log. I just want to show the string (not have it interpreted as a format string).

Example string: 'here are two percents: %%'

My attempts to print it (google-chrome Version 100.0.4896.127):

  console.log('here are two percents: %%')
  // wrong output: 'here are two percents: %'

  console.log('%s', 'here are two percents: %%')
  // wrong output: 'here are two percents: %'

  console.log('%s', '%s', 'here are two percents: %%')
  // wrong output: 'here are two percents: %'

  console.log('', 'here are two percents: %%')
  // wrong output: ' here are two percents: %%'

Note the undesired leading space, in the last attempt.

How can I get the desired output 'here are two percents: %%' ?

1 Answers

For one thing, what you're seeing is a Chrome-specific problem (I'd even go as far as to call that a bug, you should report it). On Firefox, the output is much more sane:

enter image description here

However, a portable way to get your string printed as is is to use console.dir() instead, that seems to work on both Chrome and Firefox as expected.

Related