Files
30-seconds-of-code/js/s/get-type.md
Angelos Chalaris 5c913d20bd Reorganize snippets
2023-05-03 21:19:02 +03:00

522 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Type of value snippet javascript
type
pink-flowers 2020-10-19T22:49:51+03:00

Returns the native type of a value.

  • Return 'undefined' or 'null' if the value is undefined or null.
  • Otherwise, use Object.prototype.constructor and Function.prototype.name to get the name of the constructor.
const getType = v =>
  (v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name);
getType(new Set([1, 2, 3])); // 'Set'