tags:

views:

40

answers:

2

I just answered a question where I advised removing parentheses around a statement and was asked why, to which I had no answer when I realised that it caused no errors/warnings. I could only cite bad practice. But maybe I'm the one missing something...

I did my own tests:

(print('!')); // Outputs '!'
((print('!!'))); // Outputs '!!'
(1); // No output
(qwerty); // No output
(1==2); // No output
(1=2); // Syntax error
// etc...

Can someone shed some light on whats going on and of what use are 'standalone parentheses'?

+3  A: 

what use are 'standalone parentheses'?

For all intents and purposes, no use at all.

As for what's going on, they're just delimiting expressions; there's nothing special or complex about them. The reason why your (1=2) does not work is the same reason why the same without parentheses would not work: you can't assign a value (2) to a number (1).

salathe
+2  A: 

If you put an expression inside parentheses, you get a new expression with the same value. This may be necessary where there are operators preceding or following, but otherwise it has no effect at all.

Your last example is a syntax error because

1=2

is a syntax error.

Colin Fine