Files
30-seconds-of-code/snippets/getType.md
2020-03-05 23:06:26 +02:00

471 B

title, tags
title tags
getType type,beginner

Returns the native type of a value.

Return "undefined" or "null" if the value is undefined or null. Otherwise, use Object.prototype.constructor.name to get the name of the constructor, String.prototype.toLowerCase() to return it in lowercase.

const getType = v =>
  v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
getType(new Set([1, 2, 3])); // 'set'