Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1044788 of Console.assert()

  • Revision slug: Web/API/console/assert
  • Revision title: Console.assert()
  • Revision id: 1044788
  • Created:
  • Creator: stevemao
  • Is current revision? No
  • Comment

Revision Content

{{APIRef("Console API")}}{{Non-standard_header}}

Writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.

{{AvailableInWorkers}}

Note: The console.assert() method is implemented differently in Node.js than the console.assert() method available in browsers.

Specifically, in browsers, calling console.assert() with a falsy assertion will cause the message to be printed to the console without interrupting execution of subsequent code. In Node.js, however, a falsy assertion will cause an AssertionError to be thrown.

Syntax

console.assert(assertion, obj1 [, obj2, ..., objN]);
console.assert(assertion, msg [, subst1, ..., substN]); // c-like message formatting

Parameters

assertion
Any boolean expression. If the assertion is false, the message is written to the console.
obj1 ... objN
A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.
msg
A JavaScript string containing zero or more substitution strings.
subst1 ... substN
JavaScript objects with which to replace substitution strings within msg. This parameter gives you additional control over the format of the output.

See Outputting text to the console in the documentation of {{domxref("console")}} for details.

Specifications

Specification Status Comment
{{SpecName("Console API", "#consoleassertexpression-object", "console.assert()")}} {{Spec2("Console API")}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatGeckoDesktop("28.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Available in workers {{CompatVersionUnknown}} {{CompatGeckoDesktop("38.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
c-like message formatting {{CompatNo}} {{CompatGeckoDesktop("28.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatGeckoMobile("28.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Available in workers {{CompatUnknown}} {{CompatGeckoMobile("38.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
c-like message formatting {{CompatUnknown}} {{CompatGeckoMobile("28.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

Revision Source

<div>{{APIRef("Console API")}}{{Non-standard_header}}</div>

<p>Writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.</p>

<p>{{AvailableInWorkers}}</p>

<div class="note">
<p><strong>Note:</strong>&nbsp;<em>The&nbsp;<code>console.assert()</code>&nbsp;method is implemented differently in Node.js than the&nbsp;<code>console.assert()</code>&nbsp;method&nbsp;available in browsers.</em></p>

<p>Specifically, in browsers, calling&nbsp;<code>console.assert()</code>&nbsp;with a falsy assertion will cause the&nbsp;<code>message</code>&nbsp;to be printed to the console without interrupting execution of subsequent code. In Node.js, however, a falsy assertion will cause an&nbsp;<code>AssertionError</code>&nbsp;to be thrown.</p>
</div>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
console.assert(<em>assertion</em>, <em>obj1</em> [, <em>obj2</em>, ..., <em>objN</em>]);
console.assert(<em>assertion</em>, <em>msg</em> [, <em>subst1</em>, ..., <em>substN</em>]); // c-like message formatting
</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>assertion</code></dt>
 <dd>Any boolean expression. If the assertion is false, the message is written to the console.</dd>
 <dt><code>obj1</code> ... <code>objN</code></dt>
 <dd>A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.</dd>
 <dt><code>msg</code></dt>
 <dd>A JavaScript string containing zero or more substitution strings.</dd>
 <dt><code>subst1</code> ... <code>substN</code></dt>
 <dd>JavaScript objects with which to replace substitution strings within <code>msg</code>. This parameter gives you additional control over the format of the output.</dd>
</dl>

<p>See <a href="/en-US/docs/Web/API/console#Outputting_text_to_the_console">Outputting text to the console</a> in the documentation of {{domxref("console")}} for details.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("Console API", "#consoleassertexpression-object", "console.assert()")}}</td>
   <td>{{Spec2("Console API")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("28.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Available in workers</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("38.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>c-like message formatting</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop("28.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("28.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Available in workers</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("38.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>c-like message formatting</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("28.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="https://www.opera.com/dragonfly/documentation/console/">Opera Dragonfly documentation: Console</a></li>
 <li><a href="https://msdn.microsoft.com/library/gg589530">MSDN: Using the F12 Tools Console to View Errors and Status</a></li>
 <li><a href="https://developer.chrome.com/devtools/docs/console#assertions">Chrome Developer Tools: Using the Console</a></li>
</ul>
Revert to this revision