Javascript is somewhat different in this regard as it's very loose about it type and method definitions, and it fills in a lot of the gaps for you.
In this particular case, the two are completely equivalent. But this relies on two key notions:
- Javascript doesn't care about missing method parameters to begin with.
- You are setting the default value of the method parameter to the default value of a boolean (i.e.
false
)
If you change either of these notions, it ceases to be equivalent. Below are two examples of this.
However, before we get to the examples, I want to address your question:
Is it usually recommended to define default values for Boolean arguments?
Recommendations are not always done for technical benefit. It could be of a documentative nature.
For example, while the behavior between your two methods is exactly the same, if I were to start using your code, I would infer that someFunction(a, b, x)
isn't intended to be called with missing parameters, whereas if I see someFunction(a, b, x = false)
, I can infer that whoever designed this method specifically expected that the third parameter might be omitted.
That doesn't change how the code works, but it does change my understanding of the author's intention when designing this method, so it changes how I develop this code further (e.g. not going round and "fixing" all method calls which seem to have "forgotten" the third parameter).
So is it recommended? Well, that depends.
- As a blanket rule? No, that's silly.
- As a way to indicate that omitting this parameter in this method is expected and not a problem? Yes, that's a great way of communicating that to future readers of your code.
- As a way to actually change how the code behaves? Well, yes, but in your particular example, it doesn't quite matter since any uninitialized boolean will already be
false
anyway.
Example 1
For example, if you want x
to be true
by default. Compare these examples:
someFunctionWithoutOptionalValue(a, b, x) { console.log(x == true); }
someFunctionWithOptionalFalse(a, b, x = false) { console.log(x == true); }
someFunctionWithOptionalTrue(a, b, x = true) { console.log(x == true); }
And we run this script:
someFunctionWithoutOptionalValue(a,b);
someFunctionWithOptionalFalse(a,b);
someFunctionWithOptionalTrue(a,b);
You will notice the following output:
false
false
true
When you define your optional value to be different from what the default value already is, then this makes a change to how the code behaves.
Example 2
While this may be less relevant for you now, if we move to another language which is much stricter with its typing but also has optional parameter values (I pick C# here), then the two examples you gave also are no longer equivalent.
public void SomeFunction(int a, int b, bool x) {}
public void SomeFunctionWithOptional(int a, int b, bool x = false) { }
I'm allowed to do either of these:
SomeFunctionWithOptional(1, 2, true); // x will be true
SomeFunctionWithOptional(1, 2); // x will be false
And I'm allowed to call:
SomeFunction(1,2,true); // x will be true
But I am not allowed to omit a non-optional parameter:
SomeFunction(1,2); // COMPILER ERROR
In strongly typed languages, you must mention that a method parameter is optional before you are allowed to actually leave it empty when calling the method.