1. console.count()
The count function is used to specify the amount of times it was called. It offers an optional parameter
label
. It specified, the count will be separate for each label. A
case where the count function can be helpful is as follow: The code above counts the amount of
registered students and employees. Unsure how useful this is in a production environment but I
can see this coming in handy when debugging code during development. Try to copy and test the
code above in the browser's console tab or a transpiler.const PersonType = {
STUDENT: 'Student',
EMPLOYEE: 'Employee'
};
function registerPerson(personType) {
console.count(personType);
}
registerPerson(PersonType.STUDENT);
registerPerson(PersonType.STUDENT);
registerPerson(PersonType.EMPLOYEE);
registerPerson(PersonType.EMPLOYEE);
registerPerson(PersonType.EMPLOYEE);
/* Output:
Student: 1
Student: 2
Employee: 1
Employee: 2
Employee: 3
*/
label
to determine which label to be reset.2. console.table()
I find the
table
function specifically helpful when logging nested object types, such as array of objects. An example
usage:
const menu = [
{ dish: "Pizza", image: "🍕", price: "10$" },
{ dish: "Spaghetti", image: "🍝", price: "12$" },
{ dish: "Sushi", image: "🍣", price: "5$" },
{ dish: "Ice cream", image: "🍦", price: "3$" }
];
console.table(menu);
/* Output:
┌─────────┬─────────────┬───────┬───────┐
│ (index) │ dish │ image │ price │
├─────────┼─────────────┼───────┼───────┤
│ 0 │ 'Pizza' │ '🍕' │ '10$' │
│ 1 │ 'Spaghetti' │ '🍝' │ '12$' │
│ 2 │ 'Sushi' │ '🍣' │ '5$' │
│ 3 │ 'Ice cream' │ '🍦' │ '3$' │
└─────────┴─────────────┴───────┴───────┘
*/
▼ Array(4)
.3. console.time()
The time function is used to measure the execution time of a particular part of the program or the whole program itself. Its principles are quite similar to the count function. It also takes an optional
label
argument for the name of the timer.
const NMBR = 10000000;
console.time(`Loop over ${NMBR} items`);
for(let i = 0; i < NMBR; ++i);
console.timeEnd(`Loop over ${NMBR} items`);
/* Output:
Loop over 10000000 items: 6.733ms
*/
time()
and
timeEnd()
calls. This signifies that it took 6.733ms to do 10 millions iterations.4. console.dir()
The
dir
function list properties of a JavaScript object to the console.
console.dir(console);
/* Output
console
assert: ƒ assert()
clear: ƒ clear()
context: ƒ context()
count: ƒ count()
countReset: ƒ countReset()
createTask: ƒ createTask()
debug: ƒ debug()
dir: ƒ dir()
dirxml: ƒ dirxml()
error: ƒ error()
group: ƒ group()
groupCollapsed: ƒ groupCollapsed()
groupEnd: ƒ groupEnd()
info: ƒ info()
log: ƒ log()
memory: MemoryInfo {totalJSHeapSize: 19300000, usedJSHeapSize: 15200000, jsHeapSizeLimit: 3760000000}
profile: ƒ profile()
profileEnd: ƒ profileEnd()
table: ƒ table()
time: ƒ time()
timeEnd: ƒ timeEnd()
timeLog: ƒ timeLog()
timeStamp: ƒ timeStamp()
trace: ƒ trace()
warn: ƒ warn()
Symbol(Symbol.toStringTag): "console"
[[Prototype]]: Object
*/
5. console.group()
The console object also comes with a grouping mechanism to help organize logs in the console. This is especially helpful when the amount of logs grows and they need to be separated from each other for the sake of keeping the developers sane. See example below:
console.log("############################################");
console.group("Group 1");
console.group("Nested group 1");
console.info("Logging from nested group 1");
console.groupEnd("Nested group 1");
console.info("Now I'm under group 1");
console.groupEnd("Group 1");
console.group("Group 2");
console.groupEnd("Group 2");
console.log("############################################");
/* Output
############################################
Group 1
Nested group 1
Logging from nested group 1
Now I'm under group 1
Group 2
############################################
*/
6. Pretty-print
One of the fun things about JavaScript is that it integrates well with everything in the web and CSS is one of those things. You can style logs with CSS. For example:
console.log(
"Colors of a rainbow %cred %corange %cyellow %cgreen %cblue %cindigo %cviolet",
"color: red",
"color: orange",
"color: yellow",
"color: green",
"color: blue",
"color: indigo",
"color: violet"
);
Hopefully you learned something you didn't know from this post, or simple had a good time. I was amazed when I found these as it makes my experience with JavaScript a bit less boring. I'll definitely use some of these in my projects in the future and so should you 😉