Underscore.js | _.isElement()

_.isElement() function:

  • It is used to check whether the element is a document object model or not.
  • A document object model is the way javascript sees the data of the containing pages.
  • The Cascading style sheet (CSS) and javascript (JS) interact with Document object model (DOM).

Syntax:

_.isElement(object)

Parameters:
It takes only one argument which is the object element that needs to be checked.



Return value:
It returns the true if it is a DOM element otherwise returns false.

Examples:

  1. Passing html tag to the _.isElement() function:
    The _.isElement() function takes the element and perform the checking function. It checks whether it is a DOM element or not. Like here, the argument passed to the _.isElement() function is ‘html’ and since we know it is a DOM element so, true is returned in the output.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
         </script>
      
        <script src=
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.isElement(jQuery('html')[0]));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  2. Passing body tag to the _.isElement() function:
    In this case, we pass ‘body’ tag as an argument to the _.isElement() function. Since, we know that ‘body’ tag is a DOM element 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>
      
        <script src=
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.isElement(jQuery('body')[0]));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  3. Passing div tag to the _.isElement() function:
    In this case, we pass ‘div’ tag as an argument to the _.isElement() function. Since, we know that ‘div’ tag is a DOM element therefore the output will be true.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
       
    <head>
        <script src
        </script>
        <script src=
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.isElement(_.isElement(jQuery('div')[0])));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  4. Using and (&&) operation in the _.isElement() function:
    We can even use 2 _.isElement() functions to obtain the output like in the below example. First, both of their answers will be calculated and then an ‘and’ operation will be performed. AND operation gives true only if both the answers are true, otherwise it will give false as answer.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- Write HTML code here -->
      
    <html>
       
    <head>
        <script src
         </script>
      
        <script src=
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
         console.log(_.isElement(jQuery('html')[0]) && _.isElement(jQuery('div')[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>
  
<?-- For jquery to work include the below script --?>
<script 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