Underscore.js | _.isUndefined() with Examples

30 Jan, 2019
_.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:
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 =  "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </script></head>    <body>    <script type="text/javascript">        var a=10;        console.log(_.isUndefined(a));    </script></body>    </html>Output: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 = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </script></head>    <body>    <script type="text/javascript">        console.log(_.isUndefined(10));    </script></body>    </html>Output: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 = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </script></head>    <body>    <script type="text/javascript">        console.log(_.isUndefined(undefined));    </script></body>    </html>Output: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 = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </script></head>    <body>    <script type="text/javascript">        console.log(_.isUndefined(window.missingVariable));    </script></body>    </html>Output:
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 =  "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </script></head>    <body>    <script type="text/javascript">        var a=10;        console.log(_.isUndefined(a));    </script></body>    </html>Output:
<!-- Write HTML code here --><html>    <head>    <script src =  "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </script></head>    <body>    <script type="text/javascript">        var a=10;        console.log(_.isUndefined(a));    </script></body>    </html>
Output:
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 = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </script></head>    <body>    <script type="text/javascript">        console.log(_.isUndefined(10));    </script></body>    </html>Output:
<!-- Write HTML code here --><html>    <head>    <script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </script></head>    <body>    <script type="text/javascript">        console.log(_.isUndefined(10));    </script></body>    </html>
Output:
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 = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </script></head>    <body>    <script type="text/javascript">        console.log(_.isUndefined(undefined));    </script></body>    </html>Output:
<!-- Write HTML code here --><html>    <head>    <script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </script></head>    <body>    <script type="text/javascript">        console.log(_.isUndefined(undefined));    </script></body>    </html>
Output:
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 = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </script></head>    <body>    <script type="text/javascript">        console.log(_.isUndefined(window.missingVariable));    </script></body>    </html>Output:
<!-- Write HTML code here --><html>    <head>    <script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >    </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 ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
JavaScript - Underscore.js
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.