개발/Javascript
조건식과 등가식(Comparison Operators & Equality)
DANIEL.OH
2018. 3. 11. 14:06
조건식과 등가식(Comparison Operators & Equality)
Use
===and!==over==and!=.==이나!=보다===와!==을 사용해 주십시오.Conditional statements such as the
ifstatement evaluate their expression using coercion with theToBooleanabstract method and always follow these simple rules:- Objects evaluate to true
- Undefined evaluates to false
- Null evaluates to false
- Booleans evaluate to the value of the boolean
- Numbers evaluate to false if +0, -0, or NaN, otherwise true
- Strings evaluate to false if an empty string
'', otherwise true
if문과 같은 조건식은ToBoolean메소드에 의한 강제형변환으로 평가되어 항상 다음과 같은 심플한 룰을 따릅니다.- 오브젝트 는 true 로 평가됩니다.
- undefined 는 false 로 평가됩니다.
- null 은 false 로 평가됩니다.
- 부울값 은 boolean형의 값 으로 평가됩니다.
- 수치 는 true 로 평가됩니다. 하지만 +0, -0, or NaN 의 경우는 false 입니다.
- 문자열 은 true 로 평가됩니다. 하지만 빈문자
''의 경우는 false 입니다.
if ([0]) { // true // An array is an object, objects evaluate to true // 배열은 오브젝트이므로 true 로 평가됩니다. }
Use shortcuts.
단축형을 사용해 주십시오.
// bad if (name !== '') { // ...stuff... } // good if (name) { // ...stuff... } // bad if (collection.length > 0) { // ...stuff... } // good if (collection.length) { // ...stuff... }