Is it necessary to write else part in every if condition?

后端 未结 11 1651
-上瘾入骨i
-上瘾入骨i 2021-02-19 05:16

The question I asked might be closed, But i just want to know that is it necessary to write else part of every if condition. One of my senior programmer said me that \"you shoul

11条回答
  •  长发绾君心
    2021-02-19 05:33

    No, It's not required to write the else part for the if statement.

    In fact most of the developers prefer and recommend to avoid the else block.

    that is

    Instead of writing

    if (number >= 18) {
        let allow_user = true;
    } else {
        let allow_user = false;
    }
    

    Most of the developers prefer:

    let allow_user = false;
    if (number >= 18) {
        let allow_user = true;
    }
    

提交回复
热议问题