Saturday, November 7, 2015

Best Practice: Comparisons in JavaScript

Make your Comparisons Carefully

Make sure that your comparison expression will always be evaluated to what you expect.
Usage of == might be dangerous, when you only expect strict Boolean values.
Here are few points how to start doing better:
  • use triple-equals where the data might have multiple types;
  • use instanceof to identify the object type;
  • ensure that an object has the property (use hasOwnProperty()) before access to it;

No comments:

Post a Comment