Sunday, November 17, 2024
Google search engine
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

Recent Comments