Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concise syntax for javascript if statements without curly brackets

Tags:

javascript

php

So pragmatically, I've got a quick and dirty answer to what I'm looking for here. But why isn't using that a good idea? Why can't I find any formal documentation of it? Is it not part of the spec and standard? Is it not widely supported? Is it just because minification could break code using that syntax?

If you could point me to more comprehensive docs of the feature, I'd appreciate that. What defines the contents of the if block? Is it indentation based? If it was, that'd be interesting.

On another note, is there something similar to this syntax for if statements in PHP? I can swear that I've seen them being used here and there, but I can't find any examples off hand. Am I just crazy and it actually doesn't exist in PHP, or can those types of if blocks be used in PHP? Does such an if block support having an else as well, both in JS and PHP?

It seems that there's an indentation based one as well as a single-line based syntax as well. What can you tell me about the following?

if(condition) do_some_statement();

Thanks

like image 351
haxxerz Avatar asked Jul 27 '12 12:07

haxxerz


People also ask

Do JavaScript if statements need brackets?

Yes, it works, but only up to a single line just after an 'if' or 'else' statement. If multiple lines are required to be used then curly braces are necessary.

Can we use if else without curly braces?

Answer 560504c2e39efe0a910001bcYes it is not necessary to use curly braces after conditions and loops (functions always need them) IF and only if there is just one statement following.

Do you need curly brackets in JavaScript?

Curly braces { } are special syntax in JSX. It is used to evaluate a JavaScript expression during compilation. A JavaScript expression can be a variable, function, an object, or any code that resolves into a value.

What happens if you do not follow an IF () statement with opening and closing curly braces?

In C# curly braces are optional, but only for the first line of code. Meaning that if the statement does not have braces, only the line of code right after the if condition (the statement body) will be executed. Everything else falls outside the statement body and therefore will not be executed. Save this answer.


2 Answers

But why isn't using that a good idea?

Because it's hard to maintain.

Why can't I find any formal documentation of it? Is it not part of the spec and standard?

Of course it is, see §12.5 - The if Statement and §12 - Statements in the spec. The body of an if is a Statement. One kind of Statement is Block (§12.1), which allows a list of statements to be treated as one statement, but there are many other kinds of statements.

Is it not widely supported?

Universally.

Is it just because minification could break code using that syntax?

A good minifier won't break that syntax. (A good minifier will make use of it, in fact.)

What defines the contents of the if block? Is it indentation based?

The body of an if statement consists only of the statement following it, indentation has no significance in JavaScript. So all of these are equivalent:

if (foo)
    bar();
charlie();

if (foo) bar();
charlie();

if (foo)
bar(); charlie();

    if (foo)
bar();
    charlie();

In the above, only the call to bar is conditional on foo; charlie is called regardless.

That's why we have Block, the Statement that introduces a list of statements to be treated as a unit (a block, you might say :-) ):

if (foo) {
    bar();
}
charlie();

if (foo) { bar(); }
charlie();

if (foo) {
bar(); } charlie();

    if (foo)
{ bar(); }
    charlie();

Indentation is important for humans, though, so keeping consistent indentation is a good idea. The first example in each of the above is probably clearest (of the ones listed) for us mere mortals. :-)

On another note, is there something similar to this syntax for if statements in PHP?

I'm not a big PHP-head, but it looks identical, defined in Control Structures - if. There are examples with and without {}. (There's also a different, alternative syntax I won't go into here.)

Does such an if block support having an else as well, both in JS and PHP?

Yes, if supports else both with and without blocks.

like image 58
T.J. Crowder Avatar answered Sep 23 '22 10:09

T.J. Crowder


javascript is not white space sensitive, meaning

if(condition) do_some_statement();

is the same as

if(condition)
    do_some_statement();

that being said, omitting braces in a single line if statement is always frowned upon because it can lead to bugs if the if statement ever needs to be modified:

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

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

like image 28
jbabey Avatar answered Sep 21 '22 10:09

jbabey