Underscore.js | _.isUndefined() with Examples

_.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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- 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>

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right




My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.