Console
interface
interface ConsoleThe 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[]): voidTests that an expression is true. If not, logs an error message
condition?: booleanThe 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(): voidClears the console if the environment allows it
Example
console.clear();
count
count(label?: string): voidMaintains an internal counter for a given label, incrementing it each time the method is called
label?: stringThe label to count. Defaults to 'default'
Example
console.count('myCounter');
console.count('myCounter'); // Will show: myCounter: 2
countReset
countReset(label?: string): voidResets the counter for a given label
label?: stringThe label to reset. Defaults to 'default'
Example
console.count('myCounter');
console.countReset('myCounter'); // Resets to 0
debug
debug(...data: any[]): voidOutputs 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): voidDisplays a list of the properties of a specified object
item?: anyObject to display
options?: anyFormatting options
Example
console.dir({ name: 'object', value: 42 }, { depth: 1 });
error
error(...data: any[]): voidOutputs 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[]): voidCreates 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[]): voidCreates 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(): voidExits the current inline group in the console
Example
console.group('Group');
console.log('Grouped message');
console.groupEnd();
info
info(...data: any[]): voidOutputs 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[]): voidOutputs 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[]): voidDisplays tabular data as a table
tabularData?: anyData 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): voidStarts a timer you can use to track how long an operation takes
label?: stringTimer label. Defaults to 'default'
Example
console.time('operation');
// ... some code
console.timeEnd('operation');
timeEnd
timeEnd(label?: string): voidStops a timer that was previously started
label?: stringTimer label to stop. Defaults to 'default'
Example
console.time('operation');
// ... some code
console.timeEnd('operation'); // Prints: operation: 1234ms
timeLog
timeLog(label?: string, ...data: any[]): voidLogs the current value of a timer that was previously started
label?: stringTimer label
...data: any[]Additional data to log
Example
console.time('process');
// ... some code
console.timeLog('process', 'Checkpoint A');
trace
trace(...data: any[]): voidOutputs a stack trace to the console
...data: any[]Values to be printed to the console
Example
console.trace('Trace message');
warn
warn(...data: any[]): voidOutputs 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): voidAdds a marker to the DevTools Performance panel
label?: stringLabel for the timestamp
Example
console.timeStamp('Navigation Start');
profile
profile(label?: string): voidStarts recording a performance profile
label?: stringProfile label
Example
console.profile('Performance Profile');
// ... code to profile
console.profileEnd('Performance Profile');
profileEnd
profileEnd(label?: string): voidStops recording a performance profile
label?: stringProfile label to stop
Example
console.profile('Performance Profile');
// ... code to profile
console.profileEnd('Performance Profile');
console
variable
var console: ConsoleA 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