Console

interface

interface Console

The Console interface provides methods for logging information to the console, as well as other utility methods for debugging and inspecting code. Methods include logging, debugging, and timing functionality.

See also

Methods

assert

assert(condition?: boolean, ...data: any[]): void

Tests that an expression is true. If not, logs an error message

condition?: boolean

The expression to test for truthiness

...data: any[]

Additional arguments to be printed if the assertion fails

Example

console.assert(1 === 1, "This won't show");
console.assert(1 === 2, "This will show an error");

clear

clear(): void

Clears the console if the environment allows it

Example

console.clear();

count

count(label?: string): void

Maintains an internal counter for a given label, incrementing it each time the method is called

label?: string

The label to count. Defaults to 'default'

Example

console.count('myCounter');
console.count('myCounter'); // Will show: myCounter: 2

countReset

countReset(label?: string): void

Resets the counter for a given label

label?: string

The label to reset. Defaults to 'default'

Example

console.count('myCounter');
console.countReset('myCounter'); // Resets to 0

debug

debug(...data: any[]): void

Outputs a debugging message to the console

...data: any[]

Values to be printed to the console

Example

console.debug('Debug message', { detail: 'some data' });

dir

dir(item?: any, options?: any): void

Displays a list of the properties of a specified object

item?: any

Object to display

options?: any

Formatting options

Example

console.dir({ name: 'object', value: 42 }, { depth: 1 });

error

error(...data: any[]): void

Outputs an error message to the console. This method routes the output to stderr, unlike other console methods that route to stdout.

...data: any[]

Values to be printed to the console

Example

console.error('Error occurred:', new Error('Something went wrong'));

group

group(...data: any[]): void

Creates a new inline group in the console, indenting subsequent console messages

...data: any[]

Labels for the group

Example

console.group('Group 1');
console.log('Inside group 1');
console.groupEnd();

groupCollapsed

groupCollapsed(...data: any[]): void

Creates a new inline group in the console that is initially collapsed

...data: any[]

Labels for the group

Example

console.groupCollapsed('Details');
console.log('Hidden until expanded');
console.groupEnd();

groupEnd

groupEnd(): void

Exits the current inline group in the console

Example

console.group('Group');
console.log('Grouped message');
console.groupEnd();

info

info(...data: any[]): void

Outputs an informational message to the console

...data: any[]

Values to be printed to the console

Example

console.info('Application started', { version: '1.0.0' });

log

log(...data: any[]): void

Outputs a message to the console

...data: any[]

Values to be printed to the console

Example

console.log('Hello', 'World', 123);

table

table(tabularData?: any, properties?: string[]): void

Displays tabular data as a table

tabularData?: any

Data to be displayed in table format

properties?: string[]

Array of property names to be displayed

Example

console.table([
  { name: 'John', age: 30 },
  { name: 'Jane', age: 25 }
]);

time

time(label?: string): void

Starts a timer you can use to track how long an operation takes

label?: string

Timer label. Defaults to 'default'

Example

console.time('operation');
// ... some code
console.timeEnd('operation');

timeEnd

timeEnd(label?: string): void

Stops a timer that was previously started

label?: string

Timer label to stop. Defaults to 'default'

Example

console.time('operation');
// ... some code
console.timeEnd('operation'); // Prints: operation: 1234ms

timeLog

timeLog(label?: string, ...data: any[]): void

Logs the current value of a timer that was previously started

label?: string

Timer label

...data: any[]

Additional data to log

Example

console.time('process');
// ... some code
console.timeLog('process', 'Checkpoint A');

trace

trace(...data: any[]): void

Outputs a stack trace to the console

...data: any[]

Values to be printed to the console

Example

console.trace('Trace message');

warn

warn(...data: any[]): void

Outputs a warning message to the console

...data: any[]

Values to be printed to the console

Example

console.warn('Deprecated feature used');

timeStamp

timeStamp(label?: string): void

Adds a marker to the DevTools Performance panel

label?: string

Label for the timestamp

Example

console.timeStamp('Navigation Start');

profile

profile(label?: string): void

Starts recording a performance profile

label?: string

Profile label

Example

console.profile('Performance Profile');
// ... code to profile
console.profileEnd('Performance Profile');

profileEnd

profileEnd(label?: string): void

Stops recording a performance profile

label?: string

Profile label to stop

Example

console.profile('Performance Profile');
// ... code to profile
console.profileEnd('Performance Profile');

console

variable

var console: Console

A global console object that provides methods for logging, debugging, and error reporting. The console object provides access to the browser's or runtime's debugging console functionality. It allows developers to output text and data for debugging purposes.

Example

console.log("Hello, world!");
console.error("An error occurred");
console.warn("Warning message");
console.debug("Debug information");

See also