Files
30-seconds-of-code/snippets/get-type.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

521 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Type of value type pink-flowers 2017-12-17T17:55:51+02:00 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'