Underscore.js _.value() Function

Underscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy.
The _.value() function is used to extract the value of the wrapped object. This function is mainly used with chain function in JavaScript.

Note: It is very necessary to link the underscore CDN before going and using underscore functions in the browser. When linking the underscore.js CDN The “_” is attached to the browser as global variable.

Syntax:

_.chain(obj).value()

Parameters: This function does not accept any parameter. It is chained with different functions. “.value()” is used to extract the value of the object i.e. array, string etc.Return Value: It returns the value of the object of object type. If the object is an array it returns an array, If the object is a string it returns a string and so on.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script>
        let arr = ["c", "d", "a", "b"];
        console.log(`original array is ${arr}`);
  
        // Wrapped object is array so 
        // its values is returned
        let newArr = _.chain(arr).sort().value();
  
        // Changes made in original array.
        console.log(`original array is ${arr}`);
        console.log(`new array is ${newArr}`);
    </script>
</body>
  
</html>

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script>
        let str = "geeks for geeks"
        console.log(`original string is ${str}`);
  
        // Wrapped object is str so its
        // values is returned
        let newstr = _.chain(str).toArray()
                .reverse().join("").value();
  
        // Printing the new string
        console.log(`new string is ${newstr}`);
    </script>
</body>
  
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.