Files
30-seconds-of-code/snippets/getType.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

479 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
getType type,beginner 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.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'