This solution is generated by ChatGPT, correctness is not guaranteed.

AI solution for JavaScript Coding Question on BFE.dev
20. Detect data type in JavaScript

One possible solution to this problem is to use the typeof operator for the basic data types and check the constructor property for the complex types. Here is a sample function that implements this logic:

function detectType(value) {
  const type = typeof value;
  if (type !== 'object') {
    return type;
  }
  if (value === null) {
    return 'null';
  }
  const constructor = value.constructor;
  if (constructor === Array) {
    return 'array';
  }
  if (constructor === ArrayBuffer) {
    return 'arraybuffer';
  }
  if (constructor === Map) {
    return 'map';
  }
  if (constructor === Set) {
    return 'set';
  }
  if (constructor === Date) {
    return 'date';
  }
  if (constructor === Function) {
    return 'function';
  }
  return 'object';
}

The function first checks if the value is a basic type using the typeof operator. If it is not, it checks if the value is null, in which case it returns 'null'. Otherwise, it retrieves the constructor property of the value and compares it against the known complex types using ===. If a match is found, it returns the corresponding lowercase type string. If none of the known types match, it returns 'object'.

The function can be easily extended to handle additional types as needed, by adding more if statements with the appropriate constructor checks.