Short syntax for javascript if statements without curly braces

So pragmatic, I have a quick and dirty answer to what I am looking for here . But why is this idea not used? Why can't I find any official documentation? Is this not part of the specification and standard? Is it not widely supported? Is it just because minification can break code with this syntax?

If you could point me to more detailed documents of this function, I would appreciate it. What determines the contents of an if block? Is this indentation? If it were, it would be interesting.

In another note, is there something similar to this syntax for if in PHP? I can swear that I saw how they are used here and there, but I can not find any examples. I'm just crazy, and this doesn't really exist in PHP, or can these types of if blocks be used in PHP? Does such support support if having else , both in JS and in PHP?

There seems to be one indentation, as well as single-line syntax. What can you tell me about the following?

if(condition) do_some_statement();

thanks

+6
source share
5 answers

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.

+9
source

javascript is not space sensitive, i.e.

 if(condition) do_some_statement(); 

coincides with

 if(condition) do_some_statement(); 

believing that omits curly brackets on the same line if the operator is always disapproving, because it can lead to errors if you ever need to change the if value:

 if(condition) do_some_statement(); // someone adds another line here, without adding the braces // now you've introduced a bug 

also, is it really hard to write { } ?: P

+3
source

The statement following if is this: statement .

One of the possible forms that an operator can take is an expression of group commands enclosed in braces.

Thus, the syntax of the if is

 if ( expression ) statement 

Thus, the reason brackets improve maintainability is because they provide a boundary that is explicit in the amount of influence of the if control flow effect.

0
source

why not using this idea?

It is easy enough to add another statement and expect that it will only work if if passes

Why can't I find any official documentation?

MDN Documentation :

A statement executed if the condition is true. There can be any statement, including additional nested if statements. To execute multiple statements, use the block statement ({...}) to group these statements.

ECMA-262 (p. 89):

if (expression) Statement

There seems to be one indentation based on

Not. Just an if, then a condition, then an expression. The statement can be formatted on the same line or on the next line.

White space is not significant in JS.

0
source

This is a standard, part of the specification ( if-statement , other statements ) and is universally supported. Minimization does not violate it, because spaces do not have semantics in JS - this will even provide it for single-line operators to preserve two curly braces.

Thus, it is widely used (no problem!). This is sometimes considered unsuccessful because adding indented expressions without adding curly braces can lead to problems. In addition, this can lead to erratic behavior when used in nested ifs:

 if (false) if (whatever) ; else alert(""); 

Did you expect an alert? No, else belongs to the last if .

However, you can use it carefree for single-line statements, which, of course, will not be extended, for example return; .

0
source

Source: https://habr.com/ru/post/921490/


All Articles