DEV Community

Cover image for Going beyond the classic console.log
Techelopment
Techelopment

Posted on

Going beyond the classic console.log

The Chrome Developer Toolbar console¹ is a powerful and versatile tool that is essential for web developers .

Here is an overview of its main properties and features:

1. Viewing Messages
The console allows you to view logged messages using commands such as console.log(), console.warn(), console.error(), and console.info(). These commands help you monitor code execution and diagnose problems. [read more to find out all the uses of the object console😊]

2. Running JavaScript
The console also acts as a REPL (Read-Eval-Print Loop), allowing you to execute JavaScript directly. This is useful for testing code snippets, manipulating the DOM, and interacting with the page in real time.

3. Filtering and Grouping Messages
You can filter messages by type (errors, warnings, logs, etc.) and group them for better readability. The console also offers the option to disable grouping of similar messages.

4. Event Monitoring
The console can monitor DOM events and function calls using commands such as monitorEvents() and unmonitorEvents(). This is especially useful for debugging complex interactions.

5. Network Request Recording
The console can log XMLHttpRequest and Fetch requests, allowing you to examine network interactions directly from the console.


Viewing Messages in Chrome Console (subject console)

Let's now dive deeper into the features of the consoleMessage Viewer object, which are useful for debugging and monitoring JavaScript code.

console.log()

This command is used to log informational messages. It is the most common method of displaying debugging data and messages in the console. For example:

console.log('This is a log message');
Enter fullscreen mode Exit fullscreen mode

console.warn()

Used to record warnings. Warning messages appear with a warning icon and are stylized in yellow to attract attention. For example:

console.warn("check for latest library version");
Enter fullscreen mode Exit fullscreen mode

console.error()

This command is used to log errors. Error messages appear with an error icon and are stylized in red. For example:

console.error('variable not valued');
Enter fullscreen mode Exit fullscreen mode

console.info()

Similar to console.log(), but styled differently to indicate that the message is informational. For example:

console.info('google maps version 3.40');
Enter fullscreen mode Exit fullscreen mode

console.trace()

Using console.trace(), you can record a stack trace, which shows the sequence of function calls that led to the execution of a particular point in the code. For example:

console.trace('Stack trace')
Enter fullscreen mode Exit fullscreen mode

Practical Example

Before discovering the other methods it consoleprovides, here is an example that uses the different commands just described:

console.log('Starting debugging'); 
console.info('Additional information'); 
console.warn( 'Warning: Possible problem'); 
console.error('Error detected'); 
console.trace('Call trace');
Enter fullscreen mode Exit fullscreen mode

Let's now move on to advanced uses of console.


Advanced object features console

console.dir()

The command console.dir() displays a JSON representation of a specified object, allowing you to explore the object's properties in more detail. This is especially useful for inspecting complex objects such as DOM elements.

console.dir(document.body);

var myJsonObj = JSON.parse('{ "name":"Arnold", "surname": "Schwarzenegger", "movies": ["Hercules in New York", "Terminator", "Commando", "Predator", "Last Action Hero"]}') 
console.dir(myJsonObject)
Enter fullscreen mode Exit fullscreen mode

console.table()

The command console.table() displays data in tabular format, making it easier to read and analyze arrays of objects. You can specify which columns to display.

Example:

const people = [ 
  {firstname : 'Alan' , lastname : 'Turing' , profession : 'Mathematician'}, 
  {firstname : 'Tim' , lastname : 'Berners-Lee' , profession : 'Computer scientist'}, 
  {firstname : 'John' , lastname : 'Nash' , profession : 'Mathematician'} 
]; 

console.table(people); 
console.table(people, ["name"]); //display only the name property
Enter fullscreen mode Exit fullscreen mode

console.assert()

The command console.assert() logs an error message to the console if a specified expression evaluates to false. This is useful for testing conditions while code is running.

Example:

const x = 5 ; 
console.assert(x > 10 , 'x is not greater than 10');
Enter fullscreen mode Exit fullscreen mode

console.count()

The command console.count() keeps track of the number of times it is called with a specified label. It is useful for counting how many times a certain block of code is executed.

Example — This command will display the number of times the counter has been incremented:

console.count('Counter'); 
console.count('Counter');
Enter fullscreen mode Exit fullscreen mode

console.group() and console.groupEnd()

These commands allow you to group messages in the console, improving organization and readability.

Example:

console.group('Message Group'); 
console.log('Message 1'); 
console.log('Message 2'); 
console.groupEnd();
Enter fullscreen mode Exit fullscreen mode

Remember that…

Message Filtering

The console allows you to filter messages by type (log, warning, error, etc.), making it easy to find specific types of messages. This is especially useful when working with large amounts of output.

Viewing the Source Code

By clicking on a message in the console, you can view the source code that generated that message. This helps you quickly identify where in the code there is a problem.


Follow me #techelopment

Medium: @techelopment
Dev.to: Techelopment
facebook: Techelopment
instagram: @techelopment
X: techelopment
telegram: techelopment_channel
youtube: @techelopment
whatsapp: Techelopment


References
[1] Console Features Reference | Chrome DevTools | Chrome for Developers

Top comments (0)