
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
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)