Open In App

Underscore.js _.isNull() Function

Last Updated : 14 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Underscore.js _.isNull() function It is used to find whether the value of the object is null. If an object has a null value then the output will be true otherwise false. We can even perform addition, subtraction, etc operations in this function.

Syntax: 

_.isNull(object);

Parameters:

It takes only one argument which is the object that needs to be tested.

Return value:

It returns true if the object has a null value otherwise it returns false.

Passing a number in the _.isNull() function:

The _.isNull() function takes the parameter passed to it and then checks whether the object has a null value or not. In this case, since the value is a defined number “10”, so the output is not null. Therefore, the output will be false.

Example: This example shows the Passing of a number in the _.isNull() function.

HTML




<html>
   
<head>
    <script src =
    </script>
</head>
   
<body>
    <script type="text/javascript">
        let a=10;
        console.log(_.isNull(10));
    </script>
</body>
   
</html>


Output:

Passing “null” to the _.isNull() function:

Since here we have passed “null” so we need not check for the object. We know that the value passed to the _.isNull() function itself has value “null”. Therefore, the output will be true.

Example: This example shows the passinf of null to the _.isNull() function.

HTML




<html>
   
<head>
    <script src =
    </script>
</head>
   
<body>
    <script type="text/javascript">
        console.log(_.isNull(null));
    </script>
</body>
   
</html>


Output:

Passing undefined” to the _.isNull() function:

The _.isNull() function takes the parameter passed to it which here is “undefined”. We know that if anything is undefined, then it’s value will be null. And hence, the answer is true.

Example: This example shows the passing of undefined value to the _.isNull() function.

HTML




<html>
   
<head>
    <script src =
    </script>
</head>
   
<body>
    <script type="text/javascript">
        console.log(_.isNull(undefined));
    </script>
</body>
   
</html>


Output:

Performing operations on the output of _.isNull() function:

In this first we have directly stored the output of both the above 2 examples (2, 3) in variables a and b, and then we have done an addition operation in both the results. Finally, stored it in the third variable. Since the output of _.isNull() is false when we passed and true when we passed null, therefore false is stored in ‘a’ variable and true is stored in ‘b’ variable. Now if we perform addition (+) operation on both the ‘a’, ‘b’ variables then we will have true as ‘b’ is true. Hence ‘c’ variable will become 1.

Example: This example shows the performance of opertions on the output of _.isNull() function.

HTML




<html>
   
<head>
    <script src =
    </script>
</head>
   
<body>
    <script type="text/javascript">
        let a = _.isNull(undefined);
        let b = _.isNull(null);
        let c = a + b;
        console.log(a);
        console.log(b);
        console.log(c);
    </script>
</body>
   
</html>


Output:

NOTE: These commands will not work in Google console or in firefox as for these additional files need to be added which they didn’t have added. So, add the given links to your HTML file and then run them. The links are as follows: 

<script type="text/javascript" src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads