diff --git a/doc/api/console.md b/doc/api/console.md index 84b7e2c11dac8e..76f6c6ac4623a7 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -103,70 +103,33 @@ The global `console` is a special `Console` whose output is sent to new Console(process.stdout, process.stderr); ``` -### console.assert(value[, message][, ...args]) +### console.assert(value[, ...message]) -* `value` {any} -* `message` {any} -* `...args` {any} +* `value` {any} The value tested for being truthy. +* `...message` {any} All arguments besides `value` are used as error message. A simple assertion test that verifies whether `value` is truthy. If it is not, -an `AssertionError` is thrown. If provided, the error `message` is formatted -using [`util.format()`][] and used as the error message. +`Assertion failed` is logged. If provided, the error `message` is formatted +using [`util.format()`][] by passing along all message arguments. The output is +used as the error message. ```js console.assert(true, 'does nothing'); // OK -console.assert(false, 'Whoops %s', 'didn\'t work'); -// AssertionError: Whoops didn't work +console.assert(false, 'Whoops %s work', 'didn\'t'); +// Assertion failed: Whoops didn't work ``` -*Note*: The `console.assert()` method is implemented differently in Node.js -than the `console.assert()` method [available in browsers][web-api-assert]. - -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. - -Functionality approximating that implemented by browsers can be implemented -by extending Node.js' `console` and overriding the `console.assert()` method. - -In the following example, a simple module is created that extends and overrides -the default behavior of `console` in Node.js. - - -```js -'use strict'; - -// Creates a simple extension of console with a -// new impl for assert without monkey-patching. -const myConsole = Object.create(console, { - assert: { - value: function assert(assertion, message, ...args) { - try { - console.assert(assertion, message, ...args); - } catch (err) { - console.error(err.stack); - } - }, - configurable: true, - enumerable: true, - writable: true, - }, -}); - -module.exports = myConsole; -``` - -This can then be used as a direct replacement for the built in console: - -```js -const console = require('./myConsole'); -console.assert(false, 'this message will print, but no error thrown'); -console.log('this will also print'); -``` +*Note*: Calling `console.assert()` with a falsy assertion will only cause the +`message` to be printed to the console without interrupting execution of +subsequent code. ### console.clear()