What is the assert.isNull() method in Chai.js?

Overview

In Chai.js, assert.isNull() is used to check if a value is null or not.

It returns an error if the value passed is not null, and it returns nothing if the value passed is null.

Syntax

assert.isNull(val)
Syntax for assert.isNulll() nethod in Chai.js

Parameters

val: This is the value we want to check.

Return value

If the test succeeds, it returns. If the test fails, it returns an error.

Example

// import the chai module and assert interface
const {assert} = require('chai')
// function to check if values are null
const checkIfNull= (val1, val2) =>{
try{
// use the isNull() method
assert.isNull(val1)
}catch(error){
console.log(error.message)
}
}
checkIfNull(true)
checkIfNull(false)
checkIfNull(null) // test succeeded
checkIfNull(undefined)
checkIfNull(0)

Explanation

  • Line 2: We import the chai module as well as the assert.isNull() method.
  • Line 5–12: We create a function that runs the test using the assert.isNull() method. It takes the value we want to check if they are null. We use the try/catch block to catch any errors if the test fails.
  • Lines 14–18: We invoke the function and pass some values to test if they are null.
    New on Educative
    Learn to Code
    Learn any Language as a beginner
    Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
    🏆 Leaderboard
    Daily Coding Challenge
    Solve a new coding challenge every day and climb the leaderboard

    Free Resources