엄격한 비교 연산자를 사용하라!
"==" 혹은 "!="를 사용할 경우, 의도치 않게 비교 대상의 type이 변하여 비교 연산이 실행될 수 있다. 그러므로 반드시 "===" 와 "!=="를 사용하자.
나쁜 예)
// This comparison evaluates to true, because after type coercion, zero and the empty string are equal. if (0 == '') { alert('looks like they\'re equal'); }
3항 연산자 (x ? y : z)
: x가 참이면 y를, 거짓이면 z를 실행한다. 읽기는 어렵지만, 코드를 간략하게 해준다.
예)
return (actual === expected) ? 'passed' : 'FAILED ['+ testName + '] Expected "'+expected+'",but got '+'"'+actual+'"';
not 연산자(!)의 사용
일반적으로 not 연산자는 부정하는 대상 코드의 바로 앞에 붙여 사용한다.
예)
if (!isEqual) {
Boolean의 결과값을 바로 return 하라
: Boolean 값을 조건문의 결과값으로 return하는 대신 바로 return 하자.
나쁜 예)
if(charSet.size < text.length) { return false; } return true;
좋은 예)
return charSet.size > text.length;
this의 적용 (0) | 2019.04.15 |
---|---|
Closure Module Pattern (0) | 2019.04.15 |
Array Methods of Javascript (0) | 2019.04.15 |
코딩 스타일 Part 2. Quoting and Semicolons - Javascript (0) | 2019.04.13 |
코딩 스타일 Part 1. 이름 짓기 - Javascript (0) | 2019.04.13 |
댓글 영역