Concise syntax for javascript if statements without curly brackets

后端 未结 5 1586
孤街浪徒
孤街浪徒 2021-01-18 06:56

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

5条回答
  •  醉酒成梦
    2021-01-18 07:27

    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

提交回复
热议问题