Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptUnderscore.js _.isUndefined() Function

Underscore.js _.isUndefined() Function

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


Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments