Search code examples
javascriptobjectcoding-stylewrapperprimitive

JavaScript style: don't use wrapper objects for primitive types


In the Google JavaScript style guide, it says not to use wrapper objects for primitive types. It says it's "dangerous" to do so. To prove its point, it uses the example:

var x = new Boolean(false);
if (x) {
  alert('hi');  // Shows 'hi'.
}

OK, I give up. Why is the if code being executed here?


Solution

  • Because every variable that is typeof Object is truthy and wrappers are objects.