Underscore.js | _.isEmpty()

_.isEmpty() function:

  • It is used to check whether a list, array, string, object etc is empty or not.
  • It first finds out the length of the passed argument and then decides.
  • If length is zero, then the output is true otherwise false.

Syntax:

_.isEmpty(object)

Parameters:
It takes only one argument which is the object.



Return value:
It returns true if the argument passed is empty, i.e., does not have any elements in it. Otherwise it returns false.

Examples:

  1. Passing an empty element to the _.isElement() function:
    The _.isElement() function takes the element from the list one by one and starts counting the length of the array. Each time it encounters an element, it increments length by one. Then, when the array finishes, it checks if the array’s length is zero ( it returns true) or greater than zero (then, it returns false). Here, we have an empty array so 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(_.isEmpty([]));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  2. Passing an array with 6 elements to the _.isEmpty() function:
    The procedure for the check function will be same as in the above example. Here, we have 6 elements in the array which implies that at the finish of the array, it’s length will be 6. so, the length is not equal to 0 and hence 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(_.isEmpty([1, 2, 3, 4, 5, 6]));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  3. Passing a list of characters to _.isEmpty() function:
    The _.isEmpty() function will work the same as in the examples above. It implies that it does not distinguish between whether the array has numbers, characters or is empty. It will work the same on all the array and find out their length. In this example we have an array of length 4. Hence, the output will be false.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
       
    <head>
        <script src
         </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.isEmpty(['HTML', 'CSS', 'JS', 'AJAX']));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  4. Passing an element zero to the _.isEmpty() function:
    Do not get confused with the empty array and an array containing zero as an element. Since the elements is zero, so you must be thinking that the array is empty. But the array contains an element and since the _isElement() calculates the length, therefore the length of the below array will be one which is greater than zero. And hece 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">
            console.log(_.isEmpty([0]));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  5. `

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


An example is shown below:



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.



Improved By : shubham_singh