I'd like to know the difference between the following and the role of the parentheses:
foo.bar.replace(a,b)
and
(foo.bar).replace(a,b)
do the parentheses require the contained expression to be evaluated first before moving on to the replace method? I have seen this in code I am maintaining and am curious as to why it would be neccessary? E.G.
location.hash.replace(a,b)
and
(location.hash).replace(a,b)
It is not required in your examples.
The idea is indeed that the block inside the parenthesis must be evaluated before continuing..
It is needed in cases like
(new Date()).getMilliseconds()
(not really needed in this case as noted by @Teemu)
In general use this syntax to avoid using a temporary variable..
var result = 5.3 + 2.9;
console.log( result.toFixed(1) );
can become
console.log( (5.3 +2.9).toFixed(1) );
If you were to use 5.3 + 2.9.toFixed(1)
the toFixed(1)
would get applied to 2.9 only, return a string and then concatenate it with 5.3 The result would be 5.32.9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With