The console.log()
method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.
Note: This feature is available in Web Workers
The console.log()
method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.
Note: This feature is available in Web Workers
js
log(obj1) log(obj1, /* …, */ objN) log(msg) log(msg, subst1, /* …, */ substN)
obj1
… objN
A list of JavaScript objects to output. Objects are output in the order listed. Please be warned that if you log objects in the latest versions of Chrome and Firefox, what you get logged on the console is a reference to the object, which is not necessarily the 'value' of the object at the moment in time you call console.log()
, but it is the value of the object at the moment you open the console.
msg
A JavaScript string containing zero or more substitution strings.
subst1
… substN
JavaScript objects with which to replace substitution strings within msg
. This gives you additional control over the format of the output.
See Outputting text to the console in the documentation of console
for details.
None (undefined
).
Information about an object is lazily retrieved. This means that the log message shows the content of an object at the time when it's first viewed, not when it was logged. For example:
js
const obj = {}; console.log(obj); obj.prop = 123;
This will output {}
. However, if you expand the object's details, you will see prop: 123
.
If you are going to mutate your object and you want to prevent the logged information from being updated, you can deep-clone the object before logging it. A common way is to JSON.stringify()
and then JSON.parse()
it:
js
console.log(JSON.parse(JSON.stringify(obj)));
There are other alternatives that work in browsers, such as structuredClone()
, which are more effective at cloning different types of objects.
Specification |
---|
Console Standard # log |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
log |
1 | 12 | 4 | 8 | 10.5 | 3 | 4.4 | 18 | 4 | 11 | 1 | 1.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/console/log