Use the Console API to write messages to the Console from your JavaScript. SeeGet started with logging messages to the Consolefor an interactive introduction to the topic.
SeeConsole utilities API referenceif you're looking for the convenience methods likedebug(function)
or
monitorEvents(node)
which are only available from the Console.
console.assert(expression, object)
Log level:Error
Writes anerrorto the console whenexpression
evaluates tofalse
.
constx=5;
consty=3;
constreason='xisexpectedtobelessthany';
console.assert(x<y,{x,y,reason});
console.clear()
Clears the console.
console.clear();
IfPreserve Logis enabled,console.clear()
is disabled.
Alternatively, you canClear the Consoleby clicking theicon.
console.count([label])
Log level:Info
Writes the number of times thatcount()
has been invoked at the same line and with the same
label
.Callconsole.countReset([label])
to reset the count.
console.count();
console.count('coffee');
console.count();
console.count();
console.countReset([label])
Resets a count.
console.countReset();
console.countReset('coffee');
console.createTask(name)
Returns aTask
instance that associates the current stack trace with the createdtask
object. You can later use thistask
object to run a function (f
in the following example). Thetask.run(f)
executes an arbitrary payload and forwards the return value back to the caller.
// Task creation
consttask=console.createTask(name);
// Task execution
task.run(f);// instead of f();
Thetask
forms a link between the creation context and the context of the async function. This link lets DevTools show better stack traces for async operations. For more information, seeLinked Stack Traces.
console.debug(object [, object,...])
Log level:Verbose
Identical toconsole.log(object [, object,...])
except different log level.
console.debug('debug');
console.dir(object)
Log level:Info
Prints a JSON representation of the specified object.
console.dir(document.head);
console.dirxml(node)
Log level:Info
Prints an XML representation of the descendants ofnode
.
console.dirxml(document);
console.error(object [, object,...])
Log level:Error
Printsobject
to the Console, formats it as an error, and includes a stack trace.
console.error("I'msorry,Dave.I'mafraidIcan'tdothat.");
console.group(label)
Visually groups messages together untilconsole.groupEnd(label)
is called. Use
console.groupCollapsed(label)
to collapse the group when it's initially logged to the Console.
constlabel='AdolescentIrradiatedEspionageTortoises';
console.group(label);
console.info('Leo');
console.info('Mike');
console.info('Don');
console.info('Raph');
console.groupEnd(label);
Additionally, you can nest groups.
consttimeline1='NewYork2012';
consttimeline2='CampLehigh1970';
console.group(timeline1);
console.info('Mind');
console.info('Time');
console.group(timeline2);
console.info('Space');
console.info('ExtraPymParticles');
console.groupEnd(timeline2);
console.groupEnd(timeline1);
console.groupCollapsed(label)
Same asconsole.group(label)
,except the group is initially collapsed when it's logged to
theConsole.
console.groupEnd(label)
Stops visually grouping messages. Seeconsole.group
.
console.info(object [, object,...])
Log level:Info
Identical toconsole.log(object [, object,...])
.
console.info('info');
console.log(object [, object,...])
Log level:Info
Prints a message to the Console.
console.log('log');
console.table(array [, columns])
Log level:Info
Logs an array of objects as a table.
varpeople=[
{
first:'René',
last:'Magritte',
},
{
first:'Chaim',
last:'Soutine',
birthday:'18930113',
},
{
first:'Henri',
last:'Matisse',
}
];
console.table(people);
By default,console.table()
logs all table data. To display a single column or a subset of columns, you can use the second optional parameter and specify column name or names as a string or an array of strings. For example:
console.table(people,['last','birthday']);
console.time([label])
Starts a new timer. Callconsole.timeEnd([label])
to stop the timer and print the elapsed time to
the Console.
console.time();
for(vari=0;i<100000;i++){
letsquare=i**2;
}
console.timeEnd();
console.timeEnd([label])
Log level:Info
Stops a timer. Seeconsole.time()
.
console.trace()
Log level:Info
Prints a stack trace to the Console.
constfirst=()=>{second();};
constsecond=()=>{third();};
constthird=()=>{fourth();};
constfourth=()=>{console.trace();};
first();
console.warn(object [, object,...])
Log level:Warning
Prints a warning to the Console.
console.warn('warn');