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 s…


This content originally appeared on DEV Community and was authored by Techelopment

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');

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");

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');

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');

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')

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');

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)

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

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');

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');

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();

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


This content originally appeared on DEV Community and was authored by Techelopment


Print Share Comment Cite Upload Translate Updates
APA

Techelopment | Sciencx (2024-09-21T13:07:25+00:00) Going beyond the classic console.log. Retrieved from https://www.scien.cx/2024/09/21/going-beyond-the-classic-console-log-2/

MLA
" » Going beyond the classic console.log." Techelopment | Sciencx - Saturday September 21, 2024, https://www.scien.cx/2024/09/21/going-beyond-the-classic-console-log-2/
HARVARD
Techelopment | Sciencx Saturday September 21, 2024 » Going beyond the classic console.log., viewed ,<https://www.scien.cx/2024/09/21/going-beyond-the-classic-console-log-2/>
VANCOUVER
Techelopment | Sciencx - » Going beyond the classic console.log. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/21/going-beyond-the-classic-console-log-2/
CHICAGO
" » Going beyond the classic console.log." Techelopment | Sciencx - Accessed . https://www.scien.cx/2024/09/21/going-beyond-the-classic-console-log-2/
IEEE
" » Going beyond the classic console.log." Techelopment | Sciencx [Online]. Available: https://www.scien.cx/2024/09/21/going-beyond-the-classic-console-log-2/. [Accessed: ]
rf:citation
» Going beyond the classic console.log | Techelopment | Sciencx | https://www.scien.cx/2024/09/21/going-beyond-the-classic-console-log-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.