Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

What is the notEqual method of the assert module in Node.js?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The notEqual method of the assert module in Node.js uses the != operator (Abstract Equality Comparison) to check for inequality between two values.

The process is illustrated below:

Note: You can view a list of rules for the != operator here.

To use the notEqual method, you will need to install the assert module using the command prompt, as shown below:

npm install assert

After the installation is complete, you will need to import the assert module into the program, as shown below:

const assert = require('assert');

The prototype of the notEqual method is shown below:

notEqual(actual, expected[, message])

Parameters

The notEqual method takes the following parameters:

  • actual: The first of the two values to compare.

  • expected: The second of the two values to compare.

  • message: This optional parameter holds the error message in case of an AssertionError. If this parameter is left empty, a default message is assigned.

Return value

If the values are equal, then the notEqual method throws an AssertionError, and the program terminates; otherwise, execution continues as normal.

In case of an error, the message property of the AssertionError is set equal to the message parameter. If the message parameter is not provided, a default value is assigned to the message property of the AssertionError.

Note: The notEqual method also handles NaN(Not-A-Number) comparisons. If both values are NaN, the notEqual method recognizes them as being identical.

Example

The code below shows how the notEqual method works in Node.js:

const assert = require('assert');
// evaluating first expression
try{
assert.notEqual(10, 5, "Assertion Error: The values are equal.")
console.log("No error.")
}
catch(error){
console.log(error.message)
}
// evaluating second expression
try{
assert.notEqual(10, 10, "Assertion Error: The values are equal.")
console.log("No error.")
}
catch(error){
console.log(error.message)
}
// evaluating third expression
try{
assert.notEqual(10, "10", "Assertion Error: The values are equal.")
console.log("No error.")
}
catch(error){
console.log(error.message)
}

Explanation

The code above uses 33 different expressions to show the behavior of the notEqual method.

In the first expression in line 55, the actual and expected parameters are not equal, so the notEqual method does not throw any errors. Therefore, only the try branch of the try-catch block executes.

In the second expression in line 1414, the actual and expected parameters are both 1010, so an error is thrown, which triggers the catch branch of the try-catch block. The code outputs the message associated with the error, i.e., the string provided as the message parameter to the notEqual method in line 1414.

Similarly, in the third expression in line 2323, the actual and expected parameters are both 10, but they have different types. The actual parameter is an integer, whereas the expected parameter is a string. Since the notEqual method uses abstract equality comparison rules, an error is thrown, which triggers the catch branch of the try-catch block. The code outputs the message associated with the error, i.e., the string provided as the message parameter to the notEqual method in line 2323.

RELATED TAGS

node.js

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring