lupz lupz - 28 days ago 11
Javascript Question

Do extra parentheses in conditions make any sense?

I'm working with some older JavaScript code and stumbled upon the widely use of extra parentheses in conditions like so:

if ((foo)) { /* ... */ }

if (!(bar)) { /* ... */ }

if ((lorem === 'ipsum')) { /* ... */ }

if ((functionCall1()) || (functionCall2())) { /* ... */}


Does this practice make any sense? Am I unaware of a bug in an older javascript implementation?

Answer

The extra parentheses in these examples serve no purpose at all.

This code functions exactly the same, and is (arguably) more readable:

if (foo) { /* ... */ }

if (!bar) { /* ... */ }

if (lorem === 'ipsum') { /* ... */ }

if (functionCall1() || functionCall2()) { /* ... */}

There is literally no point in adding them.