• Node.js Video Tutorials

NodeJS - console.assert() Method



The Node.js console.assert() method prints an error message to the console, if the assertion is false, and if the assertion is true, nothing happens.

In this method, if we provide falsy value as a parameter, it will stop the execution of the program and tell us that the assertion we made was false. If we provide a truthy value as a parameter, it won't return or print anything.

To get a better understanding let's look into the syntax and usage of the console.assert() method of Node.js.

Syntax

Following is the syntax of the Node.js console.assert() method

console.assert(value[,…message]);

Parameters

Where this method accepts two parameters and those are discussed below.

  • value − It is the value tested, if the given value is a truthy value.

  • message − Any message or arguments passed in this parameter will be considered an error message.

Return value

This method returns nothing if the assertion we made is true. If the assertion is failed, then it will throw an error along with the error message to stderr.

The following are the truthy values −

  • true

  • {}

  • []

  • 30

  • "0"

  • "false"

  • -32

  • 12n

  • 3.14

  • -3.14

  • Infinity

  • -Infinity

Example

In the example below, we are calling the Node.js console.assert() method by passing a truthy value into the value parameter and a message to the message parameter of the method.

const console = require('console');
console.assert(true, 'This asserttion is true');

Output

As we can see from the output in the below figure, the value we passed is truthy. So this method doesn't return anything.

// Returns nothing

Example

In the example below, we are calling the Node.js console.assert() method by passing different types of truthy values; and a message to the message parameter of the method.

const console = require('console');
const emp = [];
const num = 30;
const inf = Infinity;
console.assert(emp, '"${emp}" is a truthy value');
console.assert(num, '"${num}" is a truthy value');
console.assert(inf, '"${inf}" is a truthy value')

Output

If we compile and run the above code, we get the output as shown in the figure below. The values we passed in are truthy. So this method doesn't return anything.

// Returns nothing

The following are the falsy values −

  • false

  • 0

  • -0

  • 0n

  • "", '', ``

  • null

  • undefined

  • NaN

Example

In the following example below, we are calling the console.assert() method by passing a falsy value into the value parameter and an error message to the message parameter of the method.

const console = require('console');
console.assert(false, 'This is a falsy value');

Output

As we can see from the output below, the value we passed is a falsy value. Thus, the Assertion we made failed and the method returned the output as Assertion failed along with its error message.

Assertion failed: This is a falsy value

Example

In the example below, we are calling the console.assert() method by passing different types of falsy values and an error message to the message parameter of the method.

const console = require('console');
const zero = 0;
const negzero = -0;
const emp = "";
const NULL = null;
const und = undefined;
const nan = NaN;
console.assert(zero, '"${zero}" is a falsy value');
console.assert(negzero, '"${negzero}" is a falsy value');
console.assert(emp, '"${emp}" is a falsy value');
console.assert(NULL, '"${NULL}" is a falsy value');
console.assert(und, '"${und}" is a falsy value');
console.assert(nan, '"${nan}" is a falsy value');

Output

As we can see in the output below, the Assertions we made failed and the method returned the output as Assertion failed along with its specified error message.

Assertion failed: "${zero}" is a falsy value
Assertion failed: "${negzero}" is a falsy value
Assertion failed: "${emp}" is a falsy value
Assertion failed: "${NULL}" is a falsy value
Assertion failed: "${und}" is a falsy value
Assertion failed: "${nan}" is a falsy value
nodejs_console_module.htm
Advertisements