_vetrov.dev

menuVisible =

theme = THEMES.

Thanks for checking out the site. The site is under construction, but already something you can learn about my approach to development, technology and what I write in the blog.

const menu = [

]

Website

Compiled successfully at 12/13/2024 22:46:12

Javascript, NodeJS, Tools

10 Advanced Ways to Log to the Console Besides console.log

fetch("/posts/10-advanced-ways-to-log-to-the-console-besides-console-log")

In this article, we'll look at 10 advanced ways to output information to the console that will help you improve your code and speed up javascript code development and debugging.

When you need to debug something on the frontend, you use console.log. One of the problems with using console.log can be the "cluttering" of the console, which makes it difficult to read. Below, we will discuss 10 ways to improve logging to the console:

1. console.table

Instead of simply outputting an array or object, console.table displays the data in a tabular format, making it easier to read.

1. console.table
// Output an array of objects as a table
const users = [
  { id: 1, name: 'John Doe' },
  { id: 2, name: 'Jane Doe' }
];
console.table(users);

This will output the array of users in a tabular format, with the properties of each object as columns and objects as rows.

c3439a3e-ad9f-45f3-841d-a723de349426.jpeg

2. console.group

console.group and console.groupEnd allow you to create nested collapsible groups in the console. This can be useful for organizing and structuring debugging input data, especially at different levels of nesting.

2. console.group
console.group('User Details');
console.log('Name: John Doe');
console.log('Age: 32');
console.groupEnd();

dad5d288-e780-4b7b-a8b1-8a299ddc5073.jpeg

3. console.time

The console.time and console.timeEnd methods allow you to measure the time taken to execute a block of code. This can be useful for identifying and optimizing performance bottlenecks in your code.

3. console.time
console.time('Fetching data');
fetch('https://reqres.in/api/users')
  .then(response => response.json())
  .then(data => {
    console.timeEnd('Fetching data');
    // Process the data
  });

0245222e-68b5-4eaf-97d1-cfee5a6c7cca.jpeg

4. console.assert

This method allows you to output an error message to the console if the assertion specified in the condition is false.

4. console.assert
function add(a, b) {
  return a + b;
}

// Test the add function
const result = add(2, 3);
console.assert(result === 5, 'Expected 2 + 3 = 5');

d5d63dfb-8f46-4ec4-8d34-3588157c18b3.jpeg

5. Styles for console.log

You can use the %c placeholder in your console.log statements and apply CSS styles to the console:

5. Styles for console.log
console.log('%cHello world!', 'color: red; font-weight: bold;');

8d7d1917-ef4c-4b04-a033-04e4f37c4c87.jpeg

6. console.trace

console.trace() is a JavaScript method that outputs the stack trace of function calls that led to the current function call. It can be used for debugging code to trace where an error or unexpected result occurred.

6. console.trace

To use console.trace(), simply insert it into the code where you want to see the stack trace. For example:

function foo() {
  function bar() {
    console.trace();
  }
  bar();
}

foo();

When this code is executed, you will see the stack trace in the console starting from bar and ending at console.trace.

7. console.dir

Use the console.dir method to output an object's properties in a hierarchical format.

7. console.dir
const obj = {
  id: 1,
  name: 'John Doe',
  address: {
    street: '123 Main St',
    city: 'New York',
    zip: 10001
  }
};
console.dir(obj);

This will display the properties of the obj object in a hierarchical format, allowing you to see the object's structure and all its properties and values.

ceb4053e-494a-4833-b690-f78182b1017d.jpeg

8. console.count

console.count allows you to count the number of identical messages in your code, for example.

8. console.count
console.count('hello');
console.count('world');
console.count('hello');

828eb94e-f96c-4dbd-9126-7e4309a202e0.png

9. console.clear

When you need to clear the console from unnecessary messages, you can use console.clear.

9. console.clear

10. console.profile

console.profile is a method that you can use to profile your JavaScript code. It allows you to start profiling JavaScript code and gather information about the execution time of functions. This helps you find performance bottlenecks in your code that can be improved.

10. console.profile
console.profile('MyProfile');

// Run some code that you want to measure the performance of
for (let i = 0; i < 100000; i++) {
  // Do something
}

console.profileEnd('MyProfile');

The console will output the following information:

167d6879-ec15-422a-8fee-511e31f6dae6.png