• Node.js Video Tutorials

Node.js - assert.ok() Function



The Node.js assert.ok() function is an inbuilt function of the assert module of Node.js. it is used to verify if the given value is a truthy value.

A truthy value if the on which is considered as the Boolean value true when used with the comparison operators. All values except: false , 0 , -0 , 0n , "" , null , undefined , and NaN are considered as the truthy values.

The Node.js assert.ok() function throws an AssertionError to the output when the value parameter is falsy. It won't return anything to the output if the value parameter is truthy. If no input is given to the value parameter then the function throws an AssertionError with the message 'No value argument passed to `assert.ok()`.

Syntax

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

assert.ok(value[, message]);

Parameters

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

  • value − (Required) This parameter value will be evaluated by whether it is a truthy value or not.

  • message − (optional) String or Error type can be passed as an input into this parameter.

Return Value

This function will return the AssertionError on the terminal if the value is falsy. If the value is truthy it won't return anything.

The following are some of the truthy values −

  • true

  • {}

  • []

  • 30

  • "0"

  • "false"

  • -32

  • 12n

  • 3.14

  • -3.14

  • Infinity

  • -Infinity

Example

In the following example, we are passing a truthy value to the value parameter of the Node.js assert.ok() function.

const assert = require('assert');

assert.ok({}, 'The value is truthy');

Output

If we compile and run the code, it doesn't throw AssertionError to the output. This is true because "{}" is a truthy value.

// Returns nothing

Example

In the following example, we are passing an integer value and checking its type.

const assert = require('assert');

var num = 45;
assert.ok(typeof num === 'number', 'The value is truthy');

Output

If we compile and run the code, it doesn't throw AssertionError to the output. This is true because 45 == 'number'.

// Returns nothing

The following are the falsy values −

  • false

  • 0

  • -0

  • 0n

  • "", '', ``

  • null

  • undefined

  • NaN

Example

In the following example, we are passing a falsy value to the value parameter of the function.

const assert = require('assert');

assert.ok(null, 'The value is falsy');

Output

If we compile and run the code, it will throw an AssertionError along with the text in the message to the output. This is false because "null" is a falsy value.

assert.js:269
   throw err;
   ^
   
AssertionError [ERR_ASSERTION]: The value is falsy
   at Object.<anonymous> (/home/cg/root/639c3b5c17b34/main.js:3:8)
   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 value and checking its type.

const assert = require('assert');
var text = 'Tutorialspoint';
assert.ok(typeof text === 'boolean', 'The value is falsy');

Output

If we compile and run the code, it will throw an AssertionError along with the text in the message to the output. This is false because 'Tutorialspoint' !== 'boolean.

assert.js:269
   throw err;
   ^
   
AssertionError [ERR_ASSERTION]: The value is falsy
   at Object.<anonymous> (/home/cg/root/639c3b5c17b34/main.js:4:8)
   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 falsy value to the value parameter of the function and we didn't pass any input string to the message parameter.

const assert = require('assert');

assert.ok(false);

Output

If we compile and run the code, it will throw an AssertionError with a default text message which is automatically assigned by the function. This is false because "false" is a falsy value.

assert.js:269
   throw err;
   ^
   
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
   
   assert.ok(false)
   
   at Object.<anonymous> (/home/cg/root/639c3b5c17b34/main.js:3:8)
   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 not passing any value to the value parameter of the function and we didn't pass any input string to the message parameter.

const assert = require('assert');

assert.ok();

Output

If we compile and run the code, it will throw an AssertionError as 'No value argument passed to 'assert.ok()''.

assert.js:269
   throw err;
   ^
   
AssertionError [ERR_ASSERTION]: No value argument passed to `assert.ok()`
   at Object.<anonymous> (/home/cg/root/639c3b5c17b34/main.js:3:8)
   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)
nodejs_assert_module.htm
Advertisements