Good evening! Here's our prompt for today.
Detecting JavaScript Data Types: A Universal Method
The Challenge
Imagine a universe of data types in JavaScript! It's a colorful world with characters like Array
, ArrayBuffer
, Map
, Set
, Date
, and the mysterious Function
. Your mission, should you choose to accept it, is to create a universal method that identifies these inhabitants by their data type name.
Constraints:
- The function should take only one argument—the data object.
- We want a universal approach; using multiple
if
conditions to list and detect each data type is a no-go.
Example Usage
For the function detectType()
, the following should hold true:
JAVASCRIPT
1detectType(1); // Output: 'number'
2detectType(new Map()); // Output: 'map'
3detectType([]); // Output: 'array'
4detectType(null); // Output: 'null'

Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
function detectType(data) {
return Object.prototype.toString
.call(data)
.slice(1, -1)
.split(" ")[1]
.toLowerCase();
}
​
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
We'll now take you through what you need to know.
How do I use this guide?