The console.assert() method is an inbuilt application programming interface of the console module which is used to assert value passed to it as a parameter, i.e. it checks whether the value is true or not and print an error message, if provided and failed to assert the value.
Parameters: This method has two parameters as mentioned above and described below:
- value: This parameter specifies the value to be asserted.
- messages: It specifies the messages to be used as error messages. Any parameters passed along with value will be considered as message.
Return Value: This method doesn’t return anything if the value is true. If failed to assert the value then Assertion failed is logged followed by an error message, if provided in all subsequent parameters after value in util.format(). The output is used as an error message.
Below examples illustrate the use of console.assert() method in Node.js:
Assertion failed: error message 2
Assertion failed: error at 1==1 Assertion failed: error at 1&6 Assertion failed: error at 0&&9 Assertion failed: error at a%b==1 Assertion failed: error at b>a
Note: The above program will compile and run by using the
node filename.js command.