I am dealing with this example of Javascript code:
const real_numbers_array = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
//Karkoli že je increment, sedaj postane funkcija.
const increment = (
function(){
//Namesto zgornjega "function()" dobimo "increment"
return function increment (a1,a2 = 1){
return a1 + a2;
}
})();
console.log(increment(5,2));
console.log(increment(5));
Where we have a constant increment
to which we assign an anonymous function function()
which returns function increment()
with two arguments of which second has a default value and those two arguments are summed up.
So far I understand this. But at the end there is })();
and I don't know what is the meaning of the last empty ()
.
By default this code returns this:
And if I omit the ()
I get:
So what is the point of ()
at the end? Does anonymous function function()
actually return only increment
and we add ()
to get increment()
. If this is so, why doesn't anonymous function return function as increment()
. This is a default notation for functions afterall...
Is this some sort of an arrow function treachery? =)