Type Checker

EasyUtility Functions5 min6 tests

Implement a typeOf function that returns the accurate type of any JavaScript value as a lowercase string.

Built-in typeof fails for null, arrays, dates, regex, etc. Fix that.

Example

typeOf([]);        // 'array'
typeOf(null);      // 'null'
typeOf(new Date); // 'date'
typeOf(/abc/);     // 'regexp'
Hints (3)
  1. typeof is unreliable for null, arrays, and objects.
  2. Use Object.prototype.toString.call(value) which returns [object Type].
  3. Extract the type name and lowercase it.

Topics

  • Utility Functions

Asked at

Google · Meta

Join Us
blur