-2

I have occasionally seen (on stack overflow) code snippets that omit the braces in the else clause of the if statement. I had thought (for years and years) that this was invalid and all my recent research has supported this view, and I have edited them back in where I wanted to use the code snippet myself.

Then a year of so ago, without concentrating much I reused a segment of code from stack overflow on how to efficiently extract query string parameters from href.location that (without my noticing it) ended .... else return ; i.e. no braces around the else clause.

This has worked fine in both Firefox chrome and safari but I do not know why.

At the same time I realised that the else if clause is actually an else followed by a single if statement with no braces. Is this the same logic that you do not actually need braces around a single else clause statement?

  • 1
    Omitting brackets from an `if` statement has always been valid syntax for single line statements. – James Mar 26 '14 at 12:59
  • you're going to have to show us the code snippets you're referring to. Show us the one that is valid that you thought was invalid, and the one you prefer. Also, what browsers does it work in? What browsers does it not work in? – George Stocker Mar 26 '14 at 12:59
  • Ommitting braces it perfectly valid where the condition only applies to a single line. It's not something I'd generally recommend as a good practice, but it is valid andf always has been. – Spudley Mar 26 '14 at 13:02

6 Answers6

2

Blocks are only required if you give more than 1 commands.

if(condition)
    command;
else
    command2;
//////////////////////////
if(condition){
    command;
} else
    command2;
//////////////////////////
if(condition){
    command1;
    command2;
} else command1;
Zhafur
  • 1,626
  • 1
  • 13
  • 31
1

you can skip braces since there's only ONE statement

ponciste
  • 2,231
  • 14
  • 16
0

The expression

if (condition)
    single_statement;
else
    single_statement;

and it's combinations with {...} after both if and else parts are part of the JavaScript language.

Kavka
  • 4,191
  • 16
  • 33
0

All conditional statements in js and in most languages control whether or not the next "thing" is executed, which could be a statement (e.g. return) or a block (e.g. { //code }).

Joseph Dailey
  • 4,735
  • 2
  • 15
  • 18
0

They're only required for multi-line commands. Thus it is personal preference whether to use them for single-line commands.

I myself always use the braces, even on single-line commands. It improves the readability for such small effort for me, that it's not worth leaving them out.

Pim Verlangen
  • 387
  • 1
  • 11
0

I don't know where you looked, but the standard is pretty clear on this subject:

Section 12.5:

IfStatement :
    if ( Expression ) Statement else Statement
    if ( Expression ) Statement

where a Statement is defined a level up as

Statement :
    Block
    VariableStatement
    EmptyStatement
    ExpressionStatement
    IfStatement
    IterationStatement
    ContinueStatement
    BreakStatement
    ReturnStatement
    WithStatement
    LabelledStatement
    SwitchStatement
    ThrowStatement
    TryStatement
    DebuggerStatement
filmor
  • 30,840
  • 6
  • 50
  • 48