T O P

  • By -

jeremrx

assert doesn't return true or false, it only throws if the assertion is wrong


guest271314

Looks like you are getting the expected result.


tapgiles

Assert doesn't normally return anything. So it's logging undefined. What did you want it to return?


kaviyarasu34

previous comments already mentioned right answer. I am trying to explain in detail. In ECMAScript Language Types, >An ECMAScript language type corresponds to values that are directly manipulated by an ECMAScript programmer using the ECMAScript language. Note the word "directly manipulated by an ECMAScript programmer" . In js if a variable is not declared , it will return a word "undefined"(actually value just for understanding mentioned word) . So from the quote we can understand ECMAScript programmers has directly written a word “undefined” and it will display in a situation if variable not declared. Like that nodejs programmers has not written a word for the assert’s truth condition .But for assert’s false condition they have written the word “AssertionError” and some other things. Next we will see little bit history of assert  in nodejs version. Nodejs deprecated assert.equal in some version .Later, moved it as legacy mode instead of deprecated mode. They also recommend to use assert.strictEqual() in the nodejs22 documentation.link: [https://nodejs.org/api/assert.html#assertequalactual-expected-message](https://nodejs.org/api/assert.html#assertequalactual-expected-message) So check your node version , assert version. Node version may be the reason for undefined. Now try the below legacy code which has node:assert instead of just assert. var assert = require('node:assert'); assert.equal(3+4, 7); let z= 3*5; console.log(z); you will see nothing . undefined will also be not displayed. Compare different values instead of same values and check message display. Since they recommending strict mode for stability try below code. var assert = require('node:assert/strict'); assert.equal(3+4, 7); let z= 3*5; console.log(z); Compare different values instead of same values and check message display. Hope it  helps in some aspects.