The value whose type is to be determined.
OptionalnameOnly: booleanWhether to return a simplified name instead of a full tag.
A string representing the type of the value.
This function provides a normalized type representation by combining
typeof and Object.prototype.toString semantics.
null, it returns 'null' (instead of the misleading 'object' from typeof).string, number, boolean, bigint, symbol, undefined),
it returns the result of typeof.'[object Date]', '[object Array]').nameOnly is false, it returns 'function'.nameOnly is true, it attempts to return the function or class name.When nameOnly is true, the function attempts to resolve a meaningful name:
fn.name if available (including inferred names).Function.prototype.toString.| Value | getType(x) |
getType(x, true) |
|---|---|---|
null |
'[object Null]' |
'null' |
undefined |
'undefined' |
'undefined' |
'text' |
'string' |
'string' |
123 |
'number' |
'number' |
123n |
'bigint' |
'bigint' |
[] |
'[object Array]' |
'Array' |
{} |
'[object Object]' |
'Object' |
new Date() |
'[object Date]' |
'Date' |
function foo(){} |
'function' |
'foo' |
const x = () => {} |
'function' |
'x' |
(() => {}) |
'function' |
'(anonymous)' |
class MyClass {} |
'function' |
'MyClass' |
class {} |
'function' |
'(anonymous)' |
function() {} |
'function' |
'(anonymous)' |
Function and class names may be inferred from variable bindings:
const fn = () => {};
fn.name === 'fn';
Anonymous functions or classes (without bindings nor variable names) may not have a name,
and will return '(anonymous)' as a fallback.
In minified or transpiled code, names may be altered or removed.
Returns the type of the provided value as a string.