Using "single parentheses"

I just answered a question where I advised removing the parentheses around the statement and asked why, which I didn't have an answer to when I realized it didn't throw any errors / warnings. I could only cite bad practice. But maybe I'm missing something ...

I made 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 what's going on and what "stand-alone parentheses" are?

+2


a source to share


2 answers


What is the use of "stand-alone parentheses"?

In all senses and purposes it is not used at all.



As for what happens, they are just limiting the expressions; there is nothing special or complicated about them. The reason yours (1=2)

doesn't work is for the same reason why the same thing without parentheses won't work: you can't assign a value ( 2

) to a number ( 1

).

+3


a source


If you put an expression in parentheses, you get a new expression with the same value. This may be required where there are preceding or following statements, but otherwise it has no effect.

The last example is a syntax error because



1=2

      

is a syntax error.

+2


a source







All Articles