Course contentsShow
← Web Development
Lesson 2187 of 2,98240. Browser DevTools Deep DiveFree lesson

String Substitution and Formatting

Use %s, %d, %f, %o, and %c for formatted console output with styles and interpolation.

String Substitution and Formatting

You'll learn how to use special format specifiers in console.log() to create dynamic, styled, and precisely formatted output.

Core Idea

Instead of concatenating strings manually, the browser console supports substitution tokens like %s, %d, %f, %o, and %c that act as placeholders. You place these tokens in your message string, then provide values as additional arguments—the console replaces the tokens with your formatted data.

Think of it like a template system for console messages: you define the pattern once, then plug in different values.

Available Format Specifiers

  • %s – String: converts the value to a string
  • %d or %i – Integer: formats as a whole number
  • %f – Float: formats as a decimal number
  • %o – Object: displays an interactive, inspectable object
  • %c – CSS: applies custom styles to the following text

Basic Examples

console.log('User %s has %d points', 'Alice', 1250);
// Output: User Alice has 1250 points

console.log('Pi is approximately %f', Math.PI);
// Output: Pi is approximately 3.141592653589793

console.log('Config object: %o', {theme: 'dark', lang: 'en'});
// Output: Config object: (interactive object inspector)

Styling with %c

The %c token lets you inject CSS:

console.log('%cSuccess!', 'color: green; font-weight: bold;');
console.log('%cWarning:%c something needs attention', 
  'color: orange; font-weight: bold;', 
  'color: default;');

Each %c applies styles to all text after it until the next %c or end of string.

Key Takeaway: Use %s, %d, %f, %o, and %c format specifiers to create dynamic, readable, and styled console messages without messy string concatenation.