Open In App

Underscore.js _.has() Function

Last Updated : 25 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Underscore.js _.has() function is used to check whether the given object contains the given key or not.

Syntax: 

_.has(object, key);

Parameters:

  • object: It contains the object element.
  • key: It contains the key that needs to be checked in the given object.

Return Value:

It returns a Boolean value True if the key is present in the object and False otherwise.

Example 1: This example shows the use of the underscore.js _.has() function.

HTML
<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
    </script>
</head>

<body>
    <script type="text/javascript">

        let hasFn = _.has({
            Name: 'Ashok',
            Age: 32,
            Email: '[email protected]'
        }, 'Age');

        console.log(hasFn);
    </script>
</body>

</html>

Output: 

Example 2: This example shows the use of the underscore.js _.has() function.

HTML
<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
    </script>
</head>

<body>
    <script type="text/javascript">

        let info = {
            Company: 'GeeksforGeeks',
            Address: 'Noida',
            Contact: '+91 9876543210'
        };

        console.log(_.has(info, 'Company'));
        console.log(_.has(info, 'Cont'));
    </script>
</body>

</html>

Output: 

 


Next Article

Similar Reads