frontend-challenges icon indicating copy to clipboard operation
frontend-challenges copied to clipboard

489 - Detect Data Type - javascript

Open jsartisan opened this issue 2 months ago • 0 comments

index.js

/**
 * Detect the data type of any JavaScript value.
 * @param {any} value - Value to detect type of
 * @returns {string} Lowercase type name
 */
function detectType(value) {
  // is array?
  if (Array.isArray(value)) return "array";

  // is number?
  if (typeof value === "number") return "number";

  // is boolean?
  if (typeof value === "boolean") return "boolean";

   // is string?
  if (typeof value === "string") return "string";

  // is undefined?
  if (value === undefined) return "undefined";

  // is null?
  if (value === null) return "null";

  // is symbol?
  if (typeof value === "symbol") return "symbol";

  // is bigint?
  if (typeof value === "bigint") return "bigint";

  // is map?
  if(value instanceof Map) return "map"; 

   // is set?
  if(value instanceof Set) return "set"; 

  // is date?
  if(value instanceof Date) return "date"; 

  // is arraybuffer?
  if(value instanceof ArrayBuffer) return "arraybuffer"; 

  // is function?
  if(typeof value === "function") return "function";

  return "object";

}

export { detectType };

jsartisan avatar Sep 27 '25 08:09 jsartisan