LOGGING IN JAVASCRIPT PART 4
Record Timings: You can record how long an operation took to complete using console. You start a timer with console.time and then end it with console.endTime For example, console.time('timer'); setTimeout(() => console.log, 1000); console.timeEnd('timer'); // timer: 0.1239453125ms Note: Passing the label in console.time is optional. If you use a label with console.time you must pass-in that same label when calling console.timeEnd.
Grouping Logs: You can group the console messages together with console. Use console.group and console.groupEnd to group console messages together. For example, console.group('Even Numbers'); console.log(2); console.log(4); console.log(6) console.groupEnd('Even Numbers'); Output:
Nested Grouping of Logs: Groups can also be nested to another one. Take a look at the below example, console.group('Even'); console.log('2'); console.log('4'); console.group('Odd'); console.log('1'); console.log('2'); console.groupEnd(); console.log('6'); console.groupEnd(); Output:
Styling your Logs: Console logging can be styled using the delimiter %c. The first argument is the message to be displayed. Everything that comes after the first %c will be styled by the string provided by the second argument, then everything after the next %c is styled by the following string argument, and so on. console.log( 'Hello %cJSNugget%c!', 'color: #008f68; font-weight: bold; font-size: 1rem', 'color: hotpink; font-weight: bold; font-size: 1rem;' ); Output:

Logging in JavaScript - Part-4

  • 1.
  • 2.
    Record Timings: You canrecord how long an operation took to complete using console. You start a timer with console.time and then end it with console.endTime For example, console.time('timer'); setTimeout(() => console.log, 1000); console.timeEnd('timer'); // timer: 0.1239453125ms Note: Passing the label in console.time is optional. If you use a label with console.time you must pass-in that same label when calling console.timeEnd.
  • 3.
    Grouping Logs: You cangroup the console messages together with console. Use console.group and console.groupEnd to group console messages together. For example, console.group('Even Numbers'); console.log(2); console.log(4); console.log(6) console.groupEnd('Even Numbers'); Output:
  • 4.
    Nested Grouping ofLogs: Groups can also be nested to another one. Take a look at the below example, console.group('Even'); console.log('2'); console.log('4'); console.group('Odd'); console.log('1'); console.log('2'); console.groupEnd(); console.log('6'); console.groupEnd(); Output:
  • 5.
    Styling your Logs: Consolelogging can be styled using the delimiter %c. The first argument is the message to be displayed. Everything that comes after the first %c will be styled by the string provided by the second argument, then everything after the next %c is styled by the following string argument, and so on. console.log( 'Hello %cJSNugget%c!', 'color: #008f68; font-weight: bold; font-size: 1rem', 'color: hotpink; font-weight: bold; font-size: 1rem;' ); Output: