View on GitHub

Matt's homepage

Bookmark this to keep an eye on new things I am learning.

Console

Using the console

Return Home

Console output

String output

Outputs to the terminal

console.log('my message');
console.log('my' + ' other message');

^ back to top ^

Delimetered output

Outputs to the terminal

const a = 'my';
const b = 'message';
console.log(a,b); // outputs with a space inbetween

^ back to top ^

Literals

Outputs to the terminal. Uses back-ticks and ${} syntax

const b = 'message';
console.log(`my ${b}`}; // outputs 'my message'

^ back to top ^

Clear

Clears the console

console.clear()

^ back to top ^

Grouped output

Output messages as a group with indentation

// group content auto-indented under group heading
console.group("My Settings");
console.log(mySettings.timeOutValueMS);
console.log(mySettings.isDebug);
console.log(mySettings.uniqueId);
console.groupEnd();

^ back to top ^

Output timer

Elapsed time operations

console.time("step-1");  // start timer
console.timeLog("step-1");  // current progress of timer
setTimeout(() => {
    console.log('Timer is complete');
    console.timeEnd("step-1"); // stops timer and outputs ellapsed time
}, 5000);

^ back to top ^

Table

Outputs object or arrays in a table format

const mySettings = {
    timeOutValueMS: 5000,
    isDebug: true,
    uniqueId: "27832-38927-a86d9-a7f56"
}

// console.table() - prints objects and arrays in a table format
console.table(mySettings);

^ back to top ^

Coloured output prefixes

Console Formatting - reference

import { styleText } from 'node:util';
import { errorMonitor } from 'node:events';
const danger = styleText(['yellow', 'bgRed', 'bold'], 'Error!');
const warning = styleText(['yellow', 'bgBlack', 'bold'], 'Warning');
const info = styleText(['white', 'bgBlue', 'bold'], 'Information');

console.log(danger,'oops');
console.log(warning,'uh oh');
console.log(info,'Well then..');

^ back to top ^