You are on page 1of 8

23/1/23, 9:23 JavaScript Console Tricks

BREAKING
Home
JavaScript - Generators...
Javascript Rust
   

5:02

Privacy

Home Javascript
Popular Posts
JavaScript Console
Tricks
by Shubham Guleria - December 14, 2022 0 RUST

Multithreading in Rust
by Shubham Guleria - December 12, 2022

JavaScript Console Tricks


December 14, 2022

Collections in Rust - Hash


Maps
December 07, 2022
These 5 ways and tips for console objects are essential!
Conditional Rendering in
Will console.log still serve as your primary debugging tool React
for JavaScript? November 29, 2022

It's time to brush up on your knowledge and learn how to


use the JavaScript console object to its maximum potential.

These cutting-edge techniques, such as console.table and


console.time, can help you enhance the calibre and
readability of your debug output and make it simpler to
identify and correct issues with your code.

The problem 
https://www.thealphadev.com/2022/12/javascript-console-tricks.html?utm_source=reactdigest&utm_medium&utm_campaign=1492 1/8
23/1/23, 9:23 JavaScript Console Tricks

Using only console.log has a number of drawbacks, one of


Home
which is that itJavascript Rust
can clog up your code and make it 
challenging to read. Additionally, on its own, it doesn't
provide much information. It doesn't provide any context or
additional information; it simply prints the value of
5:02
anything you feed it.

In light of this, here are eleven JavaScript console object


methods and tricks you ought to be aware of (and try out; I
know it's quicker to just use console.log, but it can improve
your debugging experience; do it for your own future!).

console.table
Facebook
With the help of this technique, you can produce tabular
data in a clear and comprehensible manner. Console.table The Alpha Dev
will display the data in a tabular style, which is simpler to 11 followers

scan and understand, as opposed to simply logging out an


array or object.
Follow Page Learn more

// Output an array of objects as a


table
const users = [
  { id: 1, name: 'John Doe' },
  { id: 2, name: 'Jane Doe' }
];
console.table(users);

The objects will be displayed as rows and the properties of


each object will be displayed as columns in a tabular
representation of the users array.

console.group
console.group and console.groupEnd. These methods allow
you to create a nested, collapsible group in the console.
Main Tags
This can be useful for organizing and structuring your
Javascript (9) MySQL (2) React (4)
debug output, so you can easily see what's happening at
different levels of your code. React Native (3) Rust (9)


https://www.thealphadev.com/2022/12/javascript-console-tricks.html?utm_source=reactdigest&utm_medium&utm_campaign=1492 2/8
23/1/23, 9:23 JavaScript Console Tricks

Home Javascript Rust


console.group('User Details');
console.log('Name: John Doe');

console.log('Age: 32');
console.groupEnd();
5:02
This will create a nested, collapsible group in the console
with the heading “User Details.” The log messages inside
the group will be indented and grouped together.

console.time
console.time and console.timeEnd. These techniques let you
calculate how long it takes a block of code to run. This
Search This Blog
might help you locate and optimize performance
bottlenecks in your code.
Search Ok

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

This will track how long it takes to parse the JSON


response and fetch data from the provided URL. The
console will display the passing time.

console.assert
With the help of this technique, you may add statements to Jan (2)

your code that must always be true—assertions. Dec (19)


Console.assert will emit an error message to the console if
Nov (5)
an assertion fails. This can help you find flaws and make
sure your code is operating as it should.

function add(a, b) {
  return a + b;
}
// Test the add function
const result = add(2, 3);

https://www.thealphadev.com/2022/12/javascript-console-tricks.html?utm_source=reactdigest&utm_medium&utm_campaign=1492 3/8
23/1/23, 9:23 JavaScript Console Tricks


console.assert(result === 5, 'Expected
Home
2 + 3 Javascript
= 5'); Rust

If the add function returns anything other than the


anticipated result of 5 when given the input values of 2 and
3, an error message will be displayed in the console.
5:02

Style your logs


Framery Soundproof Booths
In order to output styles and colours, use the console object. Framery

Your debug output will be more readable and


understandable if you use the console object, which enables
you to produce text in a variety of colours and styles.
 Report Abuse
In your console, you can use the%c placeholder. To set a
CSS style for the output text, log statements are used.

About Me
console.log('%cHello world!', 'color:
Shubham Guleria
red; font-weight: bold;'); Show more

Using the chosen CSS style, this will produce the words
"Hello world!" in bold and red.

console.trace
Create a stack trace by using the console.trace method. This
can help you understand how your code is being executed,
as well as pinpoint the source of a specific log message.

Framery Soundproof Booths


function foo() {
Framery
  console.trace();
}
function bar() {
  foo();
SHARING KNOWLEDGE WITH THE WORLD.
}
bar();

A stack trace of the function calls leading up to the


console.trace call will be printed in the console as a result.

console.dir 
https://www.thealphadev.com/2022/12/javascript-console-tricks.html?utm_source=reactdigest&utm_medium&utm_campaign=1492 4/8
23/1/23, 9:23 JavaScript Console Tricks

If you want to output an object's properties in a hierarchical


Home
style, Javascriptmethod.
use the console.dir RustThis can be helpful for 
discovering an object's structure as well as all of its
attributes and functions.

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

This will display the obj object's properties in a hierarchical


fashion, enabling you to see the object's structure as well as
all of its attributes and values.

console.count
To get the number of times a certain log message has been
output, use the console.count method. This can be helpful
for tracking the frequency with which a specific code path
is executed and for locating trouble spots in your code.

function foo(x) {
  console.count(x);
}
foo('hello');
foo('world');
foo('hello');

In the console, this will print the word "hello" and the
number 1. The word "world" will then be produced in the
console, followed by the number 1. The string "hello" will
be output once more before being followed by the number
2. (since it has been called twice).

console.clear 
https://www.thealphadev.com/2022/12/javascript-console-tricks.html?utm_source=reactdigest&utm_medium&utm_campaign=1492 5/8
23/1/23, 9:23 JavaScript Console Tricks

To clear the output from the console, use the console.clear


Home ThisJavascript
function. Rust
can help you focus on the information you're 
interested in by keeping your debug output structured and
clutter-free.

5:02
console.log('Hello world!');
console.clear();
console.log('This log message will
appear after the console is
cleared.');

This will produce a blank line after the string "Hello


world!" on the console (since the console is cleared). The
string "This log message will appear after the console is
cleaned" will then be produced.

console.profile
To evaluate the effectiveness of a piece of code, use the
console.profile and console.profileEnd methods. This might
be helpful for pinpointing performance bottlenecks and
accelerating and streamlining your code.

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

Between the console.profile and console.profileEnd calls,


this will begin profiling the block of code, and when the
console.profileEnd call is made, the results will be output in
the console. The time it took to execute the code and any
other performance-related information will be included in
the output.

Conclusion
Don't limit yourself to using console.log; the JavaScript
console object offers a wealth of additional useful and

https://www.thealphadev.com/2022/12/javascript-console-tricks.html?utm_source=reactdigest&utm_medium&utm_campaign=1492 6/8
23/1/23, 9:23 JavaScript Console Tricks

powerful tools and methods.


Home Javascript Rust 
These techniques, which range from console.table to
console.time, will assist you in enhancing the calibre and
readability of your debug output and making it simpler to
5:02
identify and resolve issues with your code.

I sincerely hope that the majority of you find the approach


covered here to be helpful. Thank you for reading, and
please feel free to leave any comments or questions in the
comments section below.

Tags Javascript

 Facebook  Twitter  

  Shubham Guleria
A full stack web developer with knowledge
of Desktop and mobile app development

You Might Like

Post A Comment

Enter comment

Previous Post Next Post 


ABOUT US
    

https://www.thealphadev.com/2022/12/javascript-console-tricks.html?utm_source=reactdigest&utm_medium&utm_campaign=1492 7/8
23/1/23, 9:23 JavaScript Console Tricks


Sharing knowledge related to
Home Javascript various
Rust
development languages.

5:02
TRANSLATE CONTACT FORM MOST POPULAR

Seleccionar idioma Multithreading in


Name
Con la tecnología de Traductor de G
Rust
December 12, 2022
Email *
MAIN TAGS
JavaScript
Message *
Console Tricks
Javascript (9) Rust (9)
December 14, 2022
React (4) React Native (3)

MySQL (2) Collections in


Rust - Hash Maps
Send
December 07, 2022

Design by Blogger | Distributed by Gooyaabi Home About Us Privacy Policy Contact Us


https://www.thealphadev.com/2022/12/javascript-console-tricks.html?utm_source=reactdigest&utm_medium&utm_campaign=1492 8/8

You might also like