Console
Using the console
Console output
String output
Outputs to the terminal
console.log('my message');
console.log('my' + ' other message');
Delimetered output
Outputs to the terminal
const a = 'my';
const b = 'message';
console.log(a,b); // outputs with a space inbetween
Literals
Outputs to the terminal. Uses back-ticks and ${} syntax
const b = 'message';
console.log(`my ${b}`}; // outputs 'my message'
Clear
Clears the console
console.clear()
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();
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);
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);
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..');