여러줄의 블록 : {} 사용 eslint: nonblock-statement-body-position
Use braces with all multi-line blocks.
복수행의 블록에는 중괄호 ({}) 를 사용해 준다.
// bad
if (test)
return false;
// good
if (test) return false;
// good
if (test) {
return false;
}
// bad
function foo() { return false; }
// good
function bar() {
return false;
}
if/else 여러줄 블록 : if 의 닫기 } 와 같은 줄에 else 두기
If you're using multi-line blocks with if and else, put else on the same line as your if block's closing brace.
복수행 블록의 if 와 else 를 이용하는 경우 else 는 if 블록 끝의 중괄호(})와 같은 행에 위치시켜 준다.
// bad
if (test) {
thing1();
thing2();
}
else {
thing3();
}
// good
if (test) {
thing1();
thing2();
} else {
thing3();
}
return 을 포함하는 if 블록 + else/else if eslint: no-else-return
- return 을 포함하는 if 블록
- 후속 else 불필요 하다.
- 후속 else if 의 리턴 -> if 리턴으로 분리한다.
// bad
function foo() {
if (x) {
return x;
} else {
return y;
}
}
// bad
function cats() {
if (x) {
return x;
} else if (y) {
return y;
}
}
// bad
function dogs() {
if (x) {
return x;
} else {
if (y) {
return y;
}
}
}
// good
function foo() {
if (x) {
return x;
}
return y;
}
// good
function cats() {
if (x) {
return x;
}
if (y) {
return y;
}
}
// good
function dogs(x) {
if (x) {
if (z) {
return y;
}
} else {
return z;
}
}
'JS > 자바스크립트 Style Guide' 카테고리의 다른 글
14. Comparison Operators & Equality (JS style guide) (0) | 2023.02.07 |
---|---|
13. Hoisting (JS style guide) (0) | 2023.01.27 |
12. Variables (JS style guide) (0) | 2023.01.26 |
11. Properties (JS style guide) (2) | 2023.01.16 |
10. terators and Generators (JS style guide) (0) | 2023.01.09 |