But why is this idea not used?
Because it is hard to maintain.
Why can't I find any official documentation? Is this not part of the specification and standard?
Of course, see & sect; 12.5 - Statement if
and & sect; 12 - Statements in the specification. The if
body is a statement. One of the types of Statement is Block ( & sect; 12.1 ), which allows you to consider the list of statements as one statement, but there are many other types of statements.
Is it not widely supported?
Universally.
Is it just because minification can break code with this syntax?
A good minifier will not break this syntax. (A good minifier will use it, essentially.)
What determines the contents of an if block? Is this indentation?
The body of the if
consists only of the statement following it; indentation does not matter in JavaScript. Thus, they are all equivalent:
if (foo) bar(); charlie(); if (foo) bar(); charlie(); if (foo) bar(); charlie(); if (foo) bar(); charlie();
In the above example, only the call to bar
depends on foo
; charlie
is invoked independently.
So, we have Block, Statement, which introduces a list of statements that will be processed as a unit (block, you can say :-)):
if (foo) { bar(); } charlie(); if (foo) { bar(); } charlie(); if (foo) { bar(); } charlie(); if (foo) { bar(); } charlie();
Indentation is important to people, however, so keeping a consistent indentation is a good idea. The first example in each of the above is probably the clearest (of those listed) for mere mortals. :-)
In another note, is there something similar to this syntax for if
in PHP?
I'm not a big PHP leader, but it looks identical, in Control Structures - if
. There are examples with and without {}
. (There also is another alternative syntax that I will not go into.)
Is such support for the if
block having else
supported in both JS and PHP?
Yes, if
supports else
both with and without blocks.