DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. JavaScript
  4. All You Can Do With the Console in JavaScript

All You Can Do With the Console in JavaScript

There's more to this object than just console.log. Read on to learn some of the other interesting funcationalites console brings to JavaScript.

Antonio Sanchez user avatar by
Antonio Sanchez
·
May. 01, 19 · Tutorial
Like (4)
Save
Tweet
Share
6.26K Views

Join the DZone community and get the full member experience.

Join For Free

You have for sure used console.log() a trillion times in your projects. But did you know all other amazing things you can do with the console when using JavaScript?

The console can do much more than printing any string or variable you pass to it: it can also measure time, start some browsers built-in tools, help you trace bugs in the code, etc.. Let’s see some of those functions.

Measure Time

When debugging your code, you will probably want to measure the performance of some functions. For this, there is no need to use the Date object or any other custom function; you can use the console directly. Measuring time in JavaScript using the console is pretty straightforward:

You have to start a timer with console.time()  passing a name as an input parameter. This will be the name of the timer. You can have up to 10.000 timers running simultaneously!

To stop the timer and print the elapsed time, use  console.timeEnd(), passing the name of the timer.

console.time('time doWhatever')
..
doWhatever()
..
console.timeEnd('time doWhatever')

You can also display the elapsed time without stopping the timer with console.timeLog(), but this will only work in some modern versions of Firefox.

Trace Your Code

Using console.log() to trace an error is extremely inefficient. Okay, you are echoing the error, but, do you know how the code got there? How was the function you are debugging executed? Which other part of your code called this function? For a better debugging experience, be sure to use console.trace(). It will print not only the message you pass as a parameter, but also the whole trace until this console.trace() was executed.

Style Your Console Output

I am sure you already know this, but, just in case, remember that you can style your output passing some CSS styles to console.log as a second parameter and the “%c” within your first parameter:

console.log(“%chere is the message”, “color: yellow; font-style: italic;“)

Using Tables in the Console

You can print your information as a table passing an array or an object to the command, console.table().

The first column and row will be labeled with the index of the array or the property names of the object being printed. Some browsers may limit the number of rows.

JavaScript Console Table

Print an Interactive List of the Properties of an Object

Use console.dir() to display all properties of any JavaScript object. If you do this on a browser, you can click on any property to extend it and have more info about it, as the properties will be printed as if it were a menu. If you want to display an XML/HTML object, better use console.dirxml(). This will output an interactive list of all the trees of the element.

For instance, if you use this on this website:

console.dirxml( document.getElementById(“page”) )

you will get this output:

Console dirxml Javascript

Too Many Outputs?

Just clear all the content using console.clear().

Do You Want More?

I have written here only the functions that I consider the most useful/interesting, but there are more. Please check the MDN web docs for more information about the console object.

Console (video game CLI) JavaScript

Published at DZone with permission of Antonio Sanchez. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Documentation 101: How to Properly Document Your Cloud Infrastructure Project
  • 4 Best dApp Frameworks for First-Time Ethereum Developers
  • Java REST API Frameworks
  • Monolithic First

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: