lodash constant and functions that return values

The lodash constant method is a method that will create a function that will return a given static constant value each time it is called. On the surface lodash constant might seem pointless, but there are some situations in which I might actually want a method like this. Say for example I have a function that expects a function as one of its arguments, I can not just pass a static value to it, so instead I would need to pass a function that will return that static value.

There are a number of built in methods that will return a static value each time it is called to begin with in lodash, but the lodash constant method is the built in way to create my one such methods. It is also true that it is not so hard to just do the same things without the use of lodash, so I will be looking at some plain old vanilla javaScirpt alternatives to using the lodash constant method also here.

1 - lodash constant basic example

So the basic idea here is that I call the lodash constant method and a new function will be returned, when doing so I pass the lodash constant method a static value that I want the resulting function to return each time it is called. So I can call the lodash constant method and pass a true boolean value to it as the first argument. The result that is returned by the lodash constant method is then a new function that will return true each time it is called.

1
2
let returnTrue = _.constant(true);
console.log( returnTrue() ); // true

2 - Functions that take functions as arguments

In javaScript I often run into functions that accept functions as there arguments. These kinds of functions are one such thing that comes to mind in which a method like the lodash constant method might come into play.

1
2
3
4
5
6
7
8
9
10
11
12
let high = (a, b) => {
a = a || _.constant(0);
b = b || _.constant(0);
return a() + b();
};
let n = high(),
n2 = high(_.constant(5), function () {
return 5;
});
console.log(n, n2); // 0 10

There are maybe a number of other situations in which there might be a need to use lodash constant, or make use of native javaScript features to create similar methods.

3 - vjs alternative with arrow functions

So it is not so hard to just do the same things that the lodash constant method does with just plai javaScript by itself these days. For example I can just use arrow functions to create simple functions that return a constant value.

1
2
3
let returnTrue = () => true;
console.log(returnTrue());

4 - vjs alternative to lodash constant with function expressions

It is not such a big deal to just do the same with old ES5 spec javaScript too when it comes to using function expressions. I can also make function expressions self executing if need be, but then what would be the point of that then right.

1
2
3
4
5
6
7
8
9
10
11
12
// is it really hard to make them with function
// expressions too when it comes to ES5 javaScript?
var returnTrue = function () {
return true;
};
console.log(returnTrue()); // true
// I can use IIFEs when it comes to making them on the spot
var n = (function () {
return 42
}
());
console.log(n); // 42

4 - Creating a noop function

So one thing that comes to mind with this is to have a way to quickly cerate a noop function, or a no operation function. I guess I could use something like the lodash constant method and pass undefined for the value, or use the lodash noop method. However why should I bother with any of those when I can just use arrow functions, or expressions, and reduce the need for lodash?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
let ticker = (opt) => {
let noop = () => undefined;
opt = opt || {};
opt.i = opt.i === undefined ? 0 : opt.i;
opt.iMax = opt.iMax === undefined ? opt.i + 10 : opt.iMax;
opt.onTick = opt.onTick || noop;
opt.onEven = opt.onEven || noop;
opt.onEnd = opt.onENd || noop;
let over = false;
return () => {
if (opt.i < opt.iMax) {
opt.onTick(opt);
if (opt.i % 2 == 0 && opt.i != 0) {
opt.onEven(opt);
}
opt.i += 1;
} else {
if (!over) {
opt.onEnd(opt);
over = true;
}
}
};
};
let t = ticker({
onEven: function (opt) {
console.log(opt.i);
}
});
let i = 10;
while (i--) {
t();
}

6 - Conclusion

So the lodash constant method is not one of those must have methods in lodash, and I think that will just become even more the case as time goes on. When it comes to methods that I might actually use in a project there are only a hand full that come to mind actually that I think I would bother with and this is not one of them.