- Node.js Tutorial
- 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 Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
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
To Continue Learning Please Login
Login with Google