Wednesday, December 24, 2025
HomeLanguagesJavascriptUnderscore.js _.result() Function

Underscore.js _.result() Function

Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects.

The _.result() function is an inbuilt function in Underscore.js library of JavaScript. Here, if the stated value of the named property is a function then you should call it with the object as a context else return it. Moreover, if a default value is stated and the property parameter is not given or is not defined then the default value will be returned.

Note: If the stated defaultValue is a function then its result will be returned as output.

Syntax:

_.result(object, property, [defaultValue])

Parameters: It accepts three parameters which are specified below:

  • object: It is the stated object.
  • property: It is the property stated.
  • [defaultValue]: It is the stated default value.

Return Value: This method returns the value of the named property.

Example 1:

Javascript




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script>
        var obj = { 
            CSportal: 'neveropen'
            gfg: function () { return 'Geeks!'; } 
        };
  
        // Calling result method with its parameters
        console.log(_.result(obj, 'CSportal'));
    </script>
</body>
  
</html>


Output:

neveropen

Example 2:

Javascript




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script>
        var obj = [1, 2, 4, 5];
  
        // Calling result method 
        // with its parameters
        console.log(_.result(obj, 9, 7));
        console.log(_.result(obj, 5));
    </script>
</body>
  
</html>


Output:

7
undefined

Reference: https://underscorejs.org/#result

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

Dominic
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6825 POSTS0 COMMENTS
Nicole Veronica
11960 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6959 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6912 POSTS0 COMMENTS
Umr Jansen
6893 POSTS0 COMMENTS