Open In App

Node.js assert() Function

Last Updated : 19 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The assert() function in Node.js is used for testing and verifying assumptions in your code. It is part of the built-in assert module, which provides a set of assertion functions to perform various checks and validations.

Node assert Function

In assert() function, if the value is not truth, then a AssertionError is thrown with a message property set equal to the value of the message parameter. 

Syntax:

assert(value[, message])

Parameters: This function accepts the following parameters as mentioned above and described below:

  • value: This parameter holds the expression that needs to be evaluated. It is of any type.
  • message: This parameter holds the error message of string or error type. It is an optional parameter.

Return Value: This function returns assertion error of object type.

Installation of assert module (optional):

You can visit the link to Install assert module. You can install this package by using this command.

npm install assert

Note: Installation is an optional step as it is inbuilt Node.js module.

After installing the assert module, you can check your assert version in command prompt using the command.

npm version assert

After that, you can just create a folder and add a file for example, index.js as shown below.

Features:

  • Part of Node.js’s standard library, so no additional installation is required.
  • Allows for custom error messages and conditions.
  • Supports a wide range of assertion types for different testing needs.

Example 1: This example uses Node.js’s assert module to assert a falsy value (0), catches the resulting error, and logs it.

// Filename: index.js 

// Requiring the module
const assert = require('assert').strict;

// Function call
try {
	assert(0)
} catch(error) {
	console.log("Error:", error)
}

Steps to run the program:

  • The project structure will look like this:
  • Run index.js file using below command:
node index.js

Output:

Error: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: assert(0) at Object. (C:\Users\Lenovo\Downloads\index.js:6:5) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: ‘ERR_ASSERTION’, actual: 0, expected: true, operator: ‘==’ }

Example 2: This example uses Node.js’s assert module to assert a truthy value (1), and if no error occurs, it logs “No Error Occurred”; otherwise, it catches and logs the error.

// Filename - index.js

// Requiring the module
const assert = require('assert').strict;

// Function call
try {
	assert(1)
	console.log("No Error Occurred")
} catch(error) {
	console.log("Error:", error)
}

Steps to run the program:

The project structure will look like this:

Run index.js file using below command:

node index.js

Output:

No Error Occurred

Summary

The assert() function in Node.js is a fundamental tool for verifying code correctness. It helps catch errors by testing conditions and ensuring that values meet expected criteria. Using assertions can improve code quality and facilitate debugging and testing processes.

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/assert.html#assert_assert_value_message


Similar Reads

three90RightbarBannerImg