-
15.1 Use
===
and!==
over==
and!=
. -
15.2 Conditional statements such as the
if
statement evaulate their expression using coercion with theToBoolean
abstract 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 ([0]) {
// true
// An array is an object, objects evaluate to true
}
- 15.3 Use shortcuts.
// bad
if (name !== '') {
// ...stuff...
}
// good
if (name) {
// ...stuff...
}
// bad
if (collection.length > 0) {
// ...stuff...
}
// good
if (collection.length) {
// ...stuff...
}
- 15.4 For more information see Truth Equality and JavaScript by Angus Croll.