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

The strictEqual method of the assert module in Node.js uses the === operator (SameValue Comparison) to check for strict equality between two elements.

Strict equality means that both the type and values of the compared elements are the same.

The process is illustrated below:

To use the strictEqual 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 strictEqual method is shown below:

strictEqual(actual, expected[, message])

Parameters

The strictEqual method takes the following parameters:

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

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

  • message: An optional parameter that 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 not strictly equal, then the strictEqual 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.

Example

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

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

Explanation

The code above uses three different expressions to show the behavior of the strictEqual method.

In the first expression in line 55, the actual and expected parameters are both integers with the value 1010, so the strictEqual 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 again 1010, but they have different types. The actual parameter is an integer, whereas the expected parameter is a string.

Since the strictEqual method requires the same types, it considers the two values unequal and throws an error, 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 strictEqual method in line 1414.

In the third expression in line 2323, the actual and expected parameters are unequal, 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 strictEqual method in line 2323.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved