Why do sometimes we need parenthesis for a function call, and sometimes we don’t?
Example:
store.dispatch(loginAction());
vs
store.dispatch(loginAction);
Somewhere I made the assumption that if it didn’t have any args passed or expect any parameters, then it didn’t need the parenthesis when called.
You always need parentheses to call a function (ie evaluate it)
The second one is just a reference to the function itself, the function is not being evaluated, it will be called at some later point in the program.
function add(a,b) {
return a + b;
}
// Call the function
add(1,2)
// Vs just assigning it to a variable to
// be used later
const example = add;
// ...later
// Call the function
example(1, 2)
This is not a correct assumption; the parentheses are not optional, without them it’s just a variable same as any other variable.
1 Like
Thank you very much for the thorough explanation.
1 Like