1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
---
title: Console
slug: Web/API/Console
translation_of: Web/API/Console
---
<div>{{APIRef}}{{Non-standard_header}}</div>
<p>The <code>console</code> object provides access to the browser's debugging console (e.g. the <a href="/en-US/docs/Tools/Web_Console">Web Console</a> in Firefox). The specifics of how it works vary from browser to browser, but there is a <em>de facto</em> set of features that are typically provided.</p>
<p>The <code>console</code> can be accessed from any global object, {{domxref("Window")}} on browsing scopes, {{domxref("WorkerGlobalScope")}} and its specific variants in workers.</p>
<p>This page documents the {{anch("Methods")}} available on the <code>console</code> object and gives a few {{anch("Usage")}} examples.</p>
<h2 id="Methods">Methods</h2>
<dl>
<dt>{{domxref("Console.assert()")}}</dt>
<dd>Log a message and stack trace to console if first argument is <code>false</code>.</dd>
<dt>{{domxref("Console.count()")}}</dt>
<dd>Log the number of times this line has been called with the given label.</dd>
<dt>{{domxref("Console.debug()")}} {{deprecated_inline}}</dt>
<dd>An alias for <code>log()</code>; this was added to improve compatibility with existing sites already using <code>debug()</code>. However, you should use {{domxref("console.log()")}} instead.</dd>
<dt>{{domxref("Console.dir()")}}</dt>
<dd>Displays an interactive listing of the properties of a specified JavaScript object. This listing lets you use disclosure triangles to examine the contents of child objects.</dd>
<dt>{{domxref("Console.error()")}}</dt>
<dd>Outputs an error message. You may use <a href="/en-US/docs/Web/API/console#Using_string_substitutions">string substitution</a> and additional arguments with this method.</dd>
<dt>{{domxref("Console._exception()")}}</dt>
<dd>An alias for <code>error();</code></dd>
<dt>{{domxref("Console.group()")}}</dt>
<dd>Creates a new inline <a href="/en-US/docs/Web/API/console#Using_groups_in_the_console">group</a>, indenting all following output by another level. To move back out a level, call <code>groupEnd()</code>.</dd>
<dt>{{domxref("Console.groupCollapsed()")}}</dt>
<dd>Creates a new inline <a href="/en-US/docs/Web/API/console#Using_groups_in_the_console">group</a>, indenting all following output by another level; unlike <code>group()</code>, this starts with the inline group collapsed, requiring the use of a disclosure button to expand it. To move back out a level, call <code>groupEnd()</code>.</dd>
<dt>{{domxref("Console.groupEnd()")}}</dt>
<dd>Exits the current inline <a href="/en-US/docs/Web/API/console#Using_groups_in_the_console">group</a>.</dd>
<dt>{{domxref("Console.info()")}}</dt>
<dd>Informative logging information. You may use <a href="/en-US/docs/Web/API/console#Using_string_substitutions">string substitution</a> and additional arguments with this method.</dd>
<dt>{{domxref("Console.log()")}}</dt>
<dd>For general output of logging information. You may use <a href="/en-US/docs/Web/API/console#Using_string_substitutions">string substitution</a> and additional arguments with this method.</dd>
<dt>{{domxref("Console.profile()")}}</dt>
<dd>Starts the <a href="/en-US/docs/Tools/Profiler">JavaScript profiler</a>. You can specify an optional label for the profile.</dd>
<dt>{{domxref("Console.profileEnd()")}}</dt>
<dd>Stops the profiler. You can see the resulting profile in the <a href="/en-US/docs/Tools/Profiler">JavaScript profiler</a>.</dd>
<dt>{{domxref("Console.table()")}}</dt>
<dd>Displays tabular data as a table.</dd>
<dt>{{domxref("Console.time()")}}</dt>
<dd>Starts a <a href="/en-US/docs/Web/API/console#Timers">timer</a> with a name specified as an input parameter. Up to 10,000 simultaneous timers can run on a given page.</dd>
<dt>{{domxref("Console.timeEnd()")}}</dt>
<dd>Stops the specified <a href="/en-US/docs/Web/API/console#Timers">timer</a> and logs the elapsed time in seconds since its start.</dd>
<dt>{{domxref("Console.trace()")}}</dt>
<dd>Outputs a <a href="/en-US/docs/Web/API/console#Stack_traces">stack trace</a>.</dd>
<dt>{{domxref("Console.warn()")}}</dt>
<dd>Outputs a warning message. You may use <a href="/en-US/docs/Web/API/console#Using_string_substitutions">string substitution</a> and additional arguments with this method.</dd>
</dl>
<h2 id="Usage" name="Usage">Usage</h2>
<h3 id="Outputting_text_to_the_console" name="Outputting_text_to_the_console">Outputting text to the console</h3>
<p>The most frequently-used feature of the console is logging of text and other data. There are four categories of output you can generate, using the {{domxref("console.log()")}}, {{domxref("console.info()")}}, {{domxref("console.warn()")}}, and {{domxref("console.error()")}} methods. Each of these results in output that's styled differently in the log, and you can use the filtering controls provided by your browser to only view the kinds of output that interest you.</p>
<p>There are two ways to use each of the output methods; you can simply pass in a list of objects whose string representations get concatenated into one string then output to the console, or you can pass in a string containing zero or more substitution strings followed by a list of the objects with which to replace them.</p>
<h4 id="Outputting_a_single_object">Outputting a single object</h4>
<p>The simplest way to use the logging methods is to output a single object:</p>
<pre class="brush: js">var someObject = { str: "Some text", id: 5 };
console.log(someObject);
</pre>
<p>The output looks something like this:</p>
<pre>[09:27:13.475] ({str:"Some text", id:5})</pre>
<h4 id="Outputting_multiple_objects">Outputting multiple objects</h4>
<p>You can also output multiple objects by simply listing them when calling the logging method, like this:</p>
<pre class="brush: js">var car = "Dodge Charger";
var someObject = {str:"Some text", id:5};
console.info("My first car was a", car, ". The object is: ", someObject);</pre>
<p>This output will look like this:</p>
<pre>[09:28:22.711] My first car was a Dodge Charger . The object is: ({str:"Some text", id:5})
</pre>
<h4 id="Using_string_substitutions">Using string substitutions</h4>
<p>Gecko 9.0 {{geckoRelease("9.0")}} introduced support for string substitutions. When passing a string to one of the console object's methods that accepts a string, you may use these substitution strings:</p>
<table class="standard-table" style="width: auto;">
<tbody>
<tr>
<td class="header">Substitution string</td>
<td class="header">Description</td>
</tr>
<tr>
<td>%o</td>
<td>Outputs a hyperlink to a JavaScript object. Clicking the link opens an inspector.</td>
</tr>
<tr>
<td>%d or %i</td>
<td>Outputs an integer. Formatting is not yet supported.</td>
</tr>
<tr>
<td>%s</td>
<td>Outputs a string.</td>
</tr>
<tr>
<td>%f</td>
<td>Outputs a floating-point value. Formatting is not yet supported.</td>
</tr>
</tbody>
</table>
<p>Each of these pulls the next argument after the format string off the parameter list. For example:</p>
<pre>for (var i=0; i<5; i++) {
console.log("Hello, %s. You've called me %d times.", "Bob", i+1);
}
</pre>
<p>The output looks like this:</p>
<pre>[13:14:13.481] Hello, Bob. You've called me 1 times.
[13:14:13.483] Hello, Bob. You've called me 2 times.
[13:14:13.485] Hello, Bob. You've called me 3 times.
[13:14:13.487] Hello, Bob. You've called me 4 times.
[13:14:13.488] Hello, Bob. You've called me 5 times.
</pre>
<h4 id="Styling_console_output">Styling console output</h4>
<p>You can use the <code>"%c"</code> directive to apply a CSS style to console output:</p>
<pre class="brush: js">console.log("%cMy stylish message", "color: red; font-style: italic");</pre>
<div><img alt="" src="https://mdn.mozillademos.org/files/7739/console-style.png" style="display: block; height: 52px; margin-left: auto; margin-right: auto; width: 293px;"></div>
<div>{{h3_gecko_minversion("Using groups in the console", "9.0")}}</div>
<p>You can use nested groups to help organize your output by visually combining related material. To create a new nested block, call <code>console.group()</code>. The <code>console.groupCollapsed()</code> method is similar, but creates the new block collapsed, requiring the use of a disclosure button to open it for reading.</p>
<div class="note"><strong>Note:</strong> Collapsed groups are not supported yet in Gecko; the <code>groupCollapsed()</code> method is the same as <code>group()</code> at this time.</div>
<p class="note">To exit the current group, simply call <code>console.groupEnd()</code>.</p>
<p class="note">For example, given this code:</p>
<pre class="brush: js">console.log("This is the outer level");
console.group();
console.log("Level 2");
console.group();
console.log("Level 3");
console.warn("More of level 3");
console.groupEnd();
console.log("Back to level 2");
console.groupEnd();
console.debug("Back to the outer level");
</pre>
<p>The output looks like this:</p>
<p><img alt="nesting.png" class="default internal" src="/@api/deki/files/6082/=nesting.png"></p>
<div>{{h3_gecko_minversion("Timers", "10.0")}}</div>
<p>In order to calculate the duration of a specific operation, Gecko 10 introduced the support of timers in the <code>console</code> object. To start a timer, call the <code>console.time</code><code>()</code> method, giving it a name as only parameter. To stop the timer, and to get the elapsed time in miliseconds, just call the <code>console.timeEnd()</code> method, again passing the timer's name as the parameter. Up to 10,000 timers can run simultaneously on a given page.</p>
<p>For example, given this code:</p>
<pre class="brush: js">console.time("answer time");
alert("Click to continue");
console.timeEnd("answer time");
</pre>
<p>will log the time needed by the user to discard the alert box:</p>
<p><img alt="timerresult.png" class="default internal" src="/@api/deki/files/6084/=timerresult.png"></p>
<p>Notice that the timer's name is displayed both when the timer is started and when it's stopped.</p>
<div class="note"><strong>Note:</strong> It's important to note that if you're using this to log the timing for network traffic, the timer will report the total time for the transaction, while the time listed in the network panel is just the amount of time required for the header. If you have response body logging enabled, the time listed for the response header and body combined should match what you see in the console output.</div>
<h3 id="Stack_traces">Stack traces</h3>
<p>The console object also supports outputting a stack trace; this will show you the call path taken to reach the point at which you call {{domxref("console.trace()")}}. Given code like this:</p>
<pre>foo();
function foo() {
function bar() {
console.trace();
}
bar();
}
</pre>
<p>The output in the console looks something like this:</p>
<p><img alt="" src="https://mdn.mozillademos.org/files/7167/api-trace2.png" style="display: block; margin-left: auto; margin-right: auto;"></p>
<h2 id="Notes">Notes</h2>
<ul>
<li>At least in Firefox, if a page defines a console object, that object overrides the one built into Firefox.</li>
<li>Prior to {{Gecko("12.0")}}, the console object's methods only work when the Web Console is open. Starting with {{Gecko("12.0")}}, output is cached until the Web Console is opened, then displayed at that time.</li>
<li>It's worth noting that the Firefox's built-in <code>console</code> object is compatible with the one provided by <a class="external" href="http://getfirebug.com/">Firebug</a>.</li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Tools" title="Tools">Tools</a></li>
<li><a href="/en-US/docs/Tools/Web_Console" title="Web Console">Web Console</a> - how the Web Console in Firefox handles console API calls</li>
<li><a href="/en-US/docs/Tools/Remote_Debugging">Remote debugging</a> - how to see console output when the debugging target is a mobile device</li>
<li><a href="/en-US/Firefox_OS/Using_the_App_Manager">Debugging apps on Firefox OS</a> - using the App Manager to debug apps running on Firefox OS</li>
<li><a href="/en-US/docs/Mozilla/Firefox_OS/Debugging/On-device_console_logging" title="/en-US/docs/Mozilla/Firefox_OS/Debugging/On-device_console_logging">On-device console logging</a> - how to do logging on Firefox OS devices</li>
</ul>
<h3 id="Other_implementations">Other implementations</h3>
<p> </p>
<ul>
<li><a href="https://developers.google.com/chrome-developer-tools/docs/console-api">Google Chrome DevTools</a>;</li>
<li><a href="http://getfirebug.com/wiki/index.php/Console_API">Firebug</a>;</li>
<li><a href="http://msdn.microsoft.com/en-us/library/hh772173(v=vs.85).aspx">Internet Explorer</a>;</li>
<li><a href="https://developer.apple.com/library/safari/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/Console/Console.html">Safari</a>.</li>
</ul>
|