Colors in JavaScript console
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
JavaScript console coloring is useful when you want certain logs to stand out during debugging. In browsers, the common technique is to use %c in console.log and pass CSS as the next argument, while in Node.js the usual approach is ANSI escape codes or a helper library.
Browser Console Styling with %c
Most modern browser developer consoles support a %c token that applies CSS styles to the following part of the log message.
This prints the word Success with the supplied CSS style. The %c token does not color the whole console session. It styles only the matching segment of that log call.
Styling Multiple Parts of One Message
You can use more than one %c marker in the same log call:
Each %c consumes one style argument. That makes it easy to emphasize labels, timings, or warnings inside a longer message.
A Small Helper Function
If you color logs often, wrap the pattern in a helper:
This keeps debugging output consistent and easier to scan.
Useful Browser Styles
Because the browser console uses CSS, you can do more than text color:
- '
font-weight' - '
background' - '
padding' - '
border-radius' - '
font-size'
Example:
The styles should still be used sparingly. Good logs become easier to scan, while overstyled logs become noisy.
Node.js Is Different
The browser %c technique does not work the same way in a plain Node.js terminal. For Node, use ANSI escape codes directly:
Or use a library such as chalk:
So when someone says “colors in JavaScript console,” the correct answer depends on whether they mean a browser console or a terminal.
Good Uses for Colored Logs
Color is most helpful when it encodes a small number of stable categories:
- success
- warning
- error
- debug module labels
If every log line uses five colors, the benefit disappears quickly. Use color as a visual cue, not as decoration.
That is especially true in large front-end applications where console output is already dense. A few predictable styles usually beat a highly customized rainbow of ad hoc log formatting during real debugging sessions for most teams in practice every day.
Common Pitfalls
One common mistake is trying browser %c styling in Node.js and expecting CSS support. That is a browser console feature, not a general JavaScript language feature.
Another issue is depending on color alone for meaning. Log text should still be understandable without the styling, especially for accessibility and copied output.
A third pitfall is overusing complex styling in high-volume logs. That makes debugging harder rather than easier.
Summary
- In browser consoles, use
%cplus CSS styles to color log output. - Multiple
%ctokens let you style different parts of one message. - In Node.js, use ANSI escape codes or a library such as
chalk. - Color works best for a few consistent log categories.
- Keep the text meaningful even without styling.
Related reading
- Combine json arrays by key, javascript
- Compare Strings Javascript Return of Likely
- Comparing kafka-node and node-rdkafka
- Component not updating after fetching new array
- Concept of promises in Java
- Configuring region in Node.js AWS SDK
- controlled async exit from phantomjs
- Convert a Unix timestamp to time in JavaScript
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.