#development #frontend #javascript #typescript

Styling your console.log

Is this necessary? Probably not, but if you want to leave an easter egg message on your portfolio website's console why not a styled one? You never know who is looking. Check out mine at stefi.codes.

To do this you would us the string substitution method that is explained below where you add a %c variable and then as the variable parameter add the styles as shown below.

1console.log(
2  "%cDebug with style with these console.log tricks",
3  "font-size:50px; background:#F9F9F9; color:#581845; padding:10px; border-radius:10px;"
4);

Warning, Errors and Info

Probably you've seen warning and errors in the console but didn't know how to add them. The info icon doesn't appear anymore therefore there is no visual difference between console.log and console.info in Chrome.

1// 4. WARNING!
2console.warn("console.warn()");
3
4// 5. ERROR :|
5console.error("console.error()");
6
7// 6. INFO
8console.info("console.info()");

Clear the console

Need a clean console. Simply run:

1console.clear();

Grouping things together together

Expanded

1console.group("Console group example");
2console.log("One");
3console.log("Two");
4console.log("Three");
5console.groupEnd("Console group example");

This can be helpful for example when looping through an object and wanting to show results in a more organized manner like below.

 1const dogs = [
 2    { name: "Ashley", age: 5 },
 3    { name: "Bruno", age: 2 },
 4    { name: "Hugo", age: 8 }
 5];
 6
 7dogs.forEach((dog) => {
 8    console.group(`${dog.name}`);
 9    console.log(`This is ${dog.name}`);
10    console.log(`${dog.name} is ${dog.age} years old`);
11    console.log(`${dog.name} is ${dog.age * 7} dog years old`);
12    console.groupEnd(`${dog.name}`);
13});

Collapsed

To get the same result but as a collapsed list you have to change console.group to console.groupCollapsed.

Keep count of console.logs

The console.count() method can be useful if you'd like to know how many times a component was rendered or maybe how many times a function was called. If you want the counter to start over the countReset can be used.

1// 11. COUNTING
2console.count("one");
3console.count("one");
4console.count("one");
5console.count("two");
6console.count("three");
7console.count("two");

Output arrays or objects as a table

Organize the output of an object of array by using the console.group() method.

 1// 13. TABLE for ARRAYS
 2const dogs = [
 3    { name: "Ashley", age: 5 },
 4    { name: "Bruno", age: 2 },
 5    { name: "Hugo", age: 8 },
 6];
 7
 8const cats = ["Juno", "Luna", "Zoe"];
 9console.table(dogs);
10console.table(cats);

String Substitution & Template Literals

Is String Substitution still used? For styling the console.log yes, but for other use case givewe can use template literals I don't think so. But here is how it do to it:

1const emoji = "🙈"
2console.log("This %s is my favorite!", emoji);

Using string substitution might have been done to avoid having to use the + to add strings together.

1const emoji = "🙈"
2console.log("This " + emoji+ " is my favorite emoji");

With template literals on can easily output this as below:

1const emoji = "🙈"
2console.log(`This ${emoji} is my favorite emoji`);