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)
typeofis unreliable for null, arrays, and objects.- Use
Object.prototype.toString.call(value)which returns[object Type]. - Extract the type name and lowercase it.
Topics
- Utility Functions
Asked at
Google · Meta
