• Node.js Video Tutorials

Node.js - assert() Function



The Node.js assert() function is used to verify whether the given value is truthy or not.

Where truthy values are the Boolean or, non-Boolean values which are treated as "true" when used with comparison operators. All the values are considered as truthy except: false, 0 , −0 , 0n , "" , null , undefined , and NaN.

This function throws an AssertionError, when the value we pass as a parameter is not false. If the value is true it won't throw any error. This function works similarly to assert.ok() function.

This is an inbuilt function of the assert module, which provides a set of assertion functions for verifying invariants.

Syntax

Following is the syntax of the Node.js assert() function

assert(value[, message]);

Parameters

This function accepts two parameters. The same are described below.

  • Value − (required) The value passed in this parameter will be evaluated for being truthy. The value can be of any type.

  • Message − (optional) String or Error type can be passed as a value to this parameter.

Return Value

This function will return the AssertionError on the terminal if the value is not true.

Example

In the example below, we are calling the Node.js assert() function by passing the Boolean value "false" as a parameter.

const assert = require('assert');
assert(false);

Output

Since the given value is falsy, when we compile and run the code, it will throw AssertionError.

node main.js
assert.js:269
   throw err;
   ^
   
AssertionError [ERR_ASSERTION]: This is a falsy value
   at Object.<anonymous> (/home/cg/root/639c16a06de3d/main.js:3:1)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Example

In the following example we are passing a string as the optional parameter (message). Along with a falsy value. Using this value, we can customize the ERR_ASSERTION statements.

const assert = require('assert');
assert(NaN, "The Given value is not a number ");

Output

assert.js:269
   throw err;
   ^
   
AssertionError [ERR_ASSERTION]: The Given value is not a number 
   at Object.<anonymous> (/home/cg/root/63b28b24b5cef/main.js:2:1)
   at Module._compile (internal/modules/cjs/loader.js:702:30)at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Example

In the example below, we are creating a function and performing the subtraction of two numbers. Then we are comparing the return value of the function with some number in the value parameter of the Node.js assert() function.

const assert = require('assert');
function subtraction(a,b){
   return a - b;
};
var output = subtraction(10 - 7);
assert(output === 4, 'Ten minus seven is four');

Output

When we compile and run the code, it will throw an AssertionError to the output. Thus the result is false.

assert.js:269
   throw err;
   ^

AssertionError [ERR_ASSERTION]: Ten minus seven is four
   at Object.<anonymous> (/home/cg/root/639c16a06de3d/main.js:7:1)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Example

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

const assert = require('assert');
assert(true, 'This is a truthy value');

Output

So when we compile and run the code, it will not throw an AssertionError to the output. Thus the result is true.

// Returns nothing

Example

In the example below, we are creating a function and performed the addition of two numbers. Then we are comparing the return value of the function with some number in the value parameter of the assert() function.

const assert = require('assert');
function add(a,b){
   return a + b;
};
var output = add(5 + 5);
assert(output === 10, 'five plus five is ten');

Output

When we compile and run the code, the function doesn't return anything on the output.

nodejs_assert_module.htm
Advertisements