In this tutorial, you will learn how to use ANSI sequences to stylize your JavaScript console output.
If you’ve ever done any programming in JavaScript, or other command line type interfaces, you’ve likely printed text to a console at some point. By default, the output you see is just plain old black text on a white background, appearing all at once.
Thankfully there is another way! With something known as ANSI escape sequences (or codes), we have more control on what the output looks like. ANSI escape sequences are a set of standardized codes that allow a programmer to style the output in console and text based applications. Whether providing needed functionality, like differentiating the categories of output with color, or just adding some flair, ANSI sequences are easy and fun to include in your programs.
All ANSI sequences must start with an escape code in order for the computer to know that the code that follows is an ANSI code. There are a few different escape codes, but we’ll be using one throughout this tutorial that is compatible in all of the popular languages like JavaScript, Python, and Java – \u001b
.
For example, here is a simple program that prints text in a few different colors. We’ll learn more about the color codes in the next section, but notice that each of the stylized lines start with the escape code.
See what we’re talking about? We just added basic colors, and the output is so much more fun! If you take a look at the above example, you’ll notice that the structure for the ANSI escape sequence is: "escape code + style code + output text"
.
Let’s explore this structure with more colors!
There are many different colors that you can use with the ANSI styling. The original 8 colors have the codes 30 - 37 for the text and 40 - 47 for the background.
Here is the code from the above example that styled the text as red with the default white background:
You’ll notice that the ANSI color sequence, following the escape code, is a square bracket, the color code, and an “m”. We could also store this in a variable for easier readability:
The original 8 text colors codes are:
You can add a background color by sandwiching a ;<background code>
in between the text color and the “m”. The code below would print text that is white on a cyan background:
The original 8 background color codes are:
Lastly, some applications will support an additional 256 color codes and/or RGB codes as well. To use the 256 color codes, your sequence will look like: "\u001b[38;5;<color code>m + output text"
for text color and "\u001b[48;5;<color code>m + output text"
for background color. Wikipedia has a nice chart of the 256 color codes (open the image in a new tab to see the larger codes):
To use RGB codes, it follows a similar format as above: "\u001b[38;2;<R code>;<G code>;<B code>m + output text"
for text color and "\u001b[48;2;<R code>;<G code>;<B code> + output text"
for background color.
Whew! So many colors! Check out the below example to see these in action. Try changing up the color codes to see a variety of outputs.
Depending on your interface, you will likely need to reset the color of your text so that all remaining text is colored as well. To do this, you use the same structure, using an ANSI code of [0m
, as in the following:
NOTE: In the CodeHS JavaScript editor, you do not need to reset the style between multiple printed lines (it resets automatically), but in the Python editor, for example, you would need to do so.
JavaScript:
Python:
You can also style text by making it bold, italicized, or underlined. Here are the ANSI sequences, with the same escape code:
\u001b[1m
\u001b[3m
\u001b[4m
NOTE: These will not show up in the CodeHS JavaScript editor (they will in the Python editor), but if you open your browser’s console, you will see the styling there.
With this code:
You’ll see this in your browser’s console: