Skip to content

Latest commit

 

History

History
196 lines (142 loc) · 4.08 KB

File metadata and controls

196 lines (142 loc) · 4.08 KB

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"
}

// 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 ^

Trace

Outputs trace information that shows the execution path to trace command

function add(...intValues) {
    console.trace('add func'); // trace called
    return intValues.reduce(getSum, 0);
}

// reducer add function
function getSum(total, currentValue) {
    return total + Math.round(currentValue);
}

console.log(add(1,2,3,4,5,6));

Trace shows the optional string value, the function, call, and module information.

Trace: add func
    at add (file:///C:/code/basics/demo.mjs:71:13)
    at file:///C:/code/basics/demo.mjs:80:13
    at ModuleJob.run (node:internal/modules/esm/module_job:437:25)
    at async node:internal/modules/esm/loader:639:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)
21

^ back to top ^

Count

Logs the numbers of times that a particulatr call to count('x') has been made.

function add(...intValues) {
    console.count('add'); // add count called
    return intValues.reduce(getSum, 0);
}

// reducer function - takes an initialValue (total) and the current value
function getSum(total, currentValue) {
    console.count('reducer'); // reducer count called
    return total + Math.round(currentValue);
}

console.log(add(1,2,3,4,5,6));

Output shows the incrementing counts.

PS C:\code\basics> node demo.mjs
add: 1
reducer: 1
reducer: 2
reducer: 3
reducer: 4
reducer: 5
reducer: 6
21

^ back to top ^