Underscore.js _.always() Method

The _.always() method takes a value and returns a function that will always return that value.

Syntax:

_.always( value );

Parameters: 

  • value: This method takes a value to be returned.

Return Value: This method returns a function.

Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed. Underscore.js contrib library can be installed using npm install underscore-contrib –save.



Example 1:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var gfgFunc = _.always('Geeks');
console.log(gfgFunc());

chevron_right


Output:

Geeks

Example 2: 

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var gfgFunc = _.always(1);
console.log("Value returned is",gfgFunc());

chevron_right


Output:

Value returned is 1

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.