Skip to content

Latest commit

 

History

History
52 lines (37 loc) · 1.18 KB

comparison-operators-equality.md

File metadata and controls

52 lines (37 loc) · 1.18 KB


Comparison Operators & Equality

  • 15.1 Use === and !== over == and !=.

  • 15.2 Conditional statements such as the if statement evaulate their expression using coercion with the ToBoolean 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...
}