Open In App

Underscore.js _.isUndefined() Function

Improve
Improve
Like Article
Like
Save
Share
Report

_.isUndefined() function:

  • It checks if the parameter passed to it is undefined or not.
  • If the parameter passed is undefined the it return true otherwise it returns false.
  • We can even pass window elements to it.

Syntax:

_.isUndefined(value)

Parameters:
It takes only one argument which is the value or the variable that needs to be checked.

Return value:
It returns true if the value or parameter passed is undefined or else it returns false.

Examples:

  1. Passing a variable to the _.isUndefined() function:
    The _.isUndefined() function takes the parameter passed to it. So, here it will check the variable ‘a’ which is passed. Since the value of ‘a’ is defined earlier as 10, so, it is a defined variable. Hence, the output will be false.




    <!-- Write HTML code here -->
    <html>
        
    <head>
        <script src
        </script>
    </head>
        
    <body>
        <script type="text/javascript">
            var a=10;
            console.log(_.isUndefined(a));
        </script>
    </body>
        
    </html>

    
    

    Output:

  2. Passing a number to the _.isUndefined() function:
    If we pass a number to the _.isUndefined() function then it checks whether that number is undefined or not. Since, we know all numbers are defined already. Therefore, the answer will be false.




    <!-- Write HTML code here -->
    <html>
        
    <head>
        <script src
        </script>
    </head>
        
    <body>
        <script type="text/javascript">
            console.log(_.isUndefined(10));
        </script>
    </body>
        
    </html>

    
    

    Output:

  3. Passing “undefined” to _.isUndefined() function:
    The _.isUndefined() function takes the element passed to it which is “undefined” here. Since the parameter passed is undefined, therefore the output will be true.




    <!-- Write HTML code here -->
    <html>
        
    <head>
        <script src
        </script>
    </head>
        
    <body>
        <script type="text/javascript">
            console.log(_.isUndefined(undefined));
        </script>
    </body>
        
    </html>

    
    

    Output:

  4. Passing missingVariable to the _.isUndefined() function:
    Here we are passing ‘window.missingVariable’ as a parameter. But here we have not defined any variable. So the missingVariable has no value. And hence, it is undefined. The output is true.




    <!-- Write HTML code here -->
    <html>
        
    <head>
        <script src
        </script>
    </head>
        
    <body>
        <script type="text/javascript">
            console.log(_.isUndefined(window.missingVariable));
        </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:




<!-- Write HTML code here -->
<script type="text/javascript" src =
</script>




Last Updated : 01 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads