Javascript console command explained

The JavaScript console is a powerful tool that allows developers to interact with a web page's code and debug issues in real-time. It can be accessed through a browser's developer tools or by using the console.log() function in a JavaScript file.

In this post, we will cover the following topics related to the JavaScript console:

  1. Accessing the console

  2. Basic console commands

  3. Formatting console output

  4. Debugging with the console

  5. Working with objects in the console

  6. Managing console logs

1. Accessing the console

The JavaScript console can be accessed in most modern browsers by using the developer tools. Here are the steps to access the console in some popular browsers:

  • Chrome: Open the Chrome menu (the three dots in the upper-right corner of the window), go to More Tools > Developer Tools, or use the keyboard shortcut Ctrl + Shift + I (on Windows) or Command + Option + I (on macOS). The console will appear as a separate panel at the bottom of the developer tools window.

  • Firefox: Open the Firefox menu (the three horizontal lines in the upper-right corner of the window), go to Web Developer > Web Console, or use the keyboard shortcut Ctrl + Shift + K (on Windows) or Command + Option + K (on macOS). The console will appear as a separate panel at the bottom of the browser window.

  • Safari: Open the Safari menu (the gear icon in the upper-right corner of the window), go to Preferences > Advanced, and check the "Show Develop menu in menu bar" option. Then, go to Develop > Show Web Inspector, or use the keyboard shortcut Option + Command + I. The console will appear as a separate panel at the bottom of the developer tools window.

2. Basic console commands

Once you have the console open, you can enter JavaScript commands directly into it and see the results in real-time. Here are some basic console commands:

  • console.log(): This function is used to print a message to the console. You can pass any value or expression as an argument to console.log(), and it will be displayed in the console. For example:
console.log('Hello, world!');
console.log(1 + 1);
console.log([1, 2, 3]);
  • console.dir(): This function is similar to console.log(), but it displays the properties of an object in a hierarchical format. For example:
const obj = {
  name: 'John',
  age: 30,
  hobbies: ['coding', 'reading']
};

console.dir(obj);
  • console.time() and console.timeEnd(): These functions can be used to measure the time it takes for a block of code to execute. The console.time() function starts a timer with a specified label, and the console.timeEnd() function stops the timer and displays the elapsed time. For example:
console.time('loop');

for (let i = 0; i < 1000000; i++) {
  // do something
}

console.timeEnd('loop');

3. Formatting console output

The console provides several ways to format the output of console.log() and other console commands. Here are some examples:

  • Using string interpolation: You can use ${} to include variables or expressions in a string passed to console.log(). For example:
const x = 5;
const y = 10;
console.log(`The value of x is ${x} and the value of y is ${y}.`);
  • Using styles: You can use the %c placeholder in a string passed to console.log() to apply CSS styles to the output. For example:
console.log('%cThis text is red.', 'color: red;');
  • Using console methods: You can use specific console methods to format the output in different ways. For example:
console.warn('This is a warning.'); // displays a warning message
console.error('This is an error.'); // displays an error message
console.info('This is some info.'); // displays an informative message
  • Using console symbols: You can use specific symbols to display icons in the console. For example:
console.log('🔥'); // displays a fire emoji
console.log('%c⚠️', 'color: orange;'); // displays a warning sign

4. Debugging with the console

The console is an essential tool for debugging JavaScript code. Here are some common techniques for debugging with the console:

  • console.assert(): This function allows you to test a boolean expression and print a message to the console if the expression evaluates to false. For example:
const x = 5;
console.assert(x === 5, 'x should be 5');
console.assert(x === 6, 'x should be 6'); // this message will be printed to the console
  • console.trace(): This function prints a stack trace to the console, showing the sequence of function calls that led to the current point in the code. For example:
function foo() {
  console.trace();
}

function bar() {
  foo();
}

bar(); // the stack trace will show that bar() called foo()
  • console.group(), console.groupCollapsed(), and console.groupEnd(): These functions allow you to create a group of console messages that can be collapsed or expanded. For example:
console.group('Group 1');
console.log('Message 1');
console.log('Message 2');
console.groupEnd();

console.groupCollapsed('Group 2');
console.log('Message 3');
console.log('Message 4');
console.groupEnd();

5. Working with objects in the console

The console provides several ways to inspect and manipulate objects. Here are some useful techniques:

  • console.table(): This function allows you to display an array of objects as a table. For example:
const users = [
  { name: 'John', age: 30 },
  { name: 'Jane', age: 25 }
];
console.table(users);
  • console.dirxml(): This function displays an XML or HTML element as a tree structure. For example:
const element = document.querySelector('#some-element');
console.dirxml(element);
  • console.count(): This function counts the number of times it has been called with a particular label. For example:
for (let i = 0; i < 10; i++) {
  console.count();
}

6. Managing console logs

The console can accumulate a large number of logs, which can make it difficult to find the information you need. Here are some tips for managing console logs:

  • Use console.clear() to clear the console.

  • Use console.group() and console.groupEnd() to create logical groups of logs.

  • Use console.table() to display data in a more organized way.

  • Use console.count() to count the number of occurrences of a particular log.

  • Use console.time() and console.timeEnd() to measure the execution time of a block of code.

I hope this post has given you a good overview of the various aspects of the JavaScript console. Happy debugging!

That's it.

Did you find this article valuable?

Support Eirik Madland by becoming a sponsor. Any amount is appreciated!