Logo

Programming-Idioms

History of Idiom 94 > diff from v37 to v38

Edit summary for version 38 by programming-idioms.org:
[JS] Split 2 ways into 2 snippets (see impl 2679)

Version 37

2019-09-01, 12:42:08

Version 38

2019-09-01, 12:43:07

Idiom #94 Print type of variable

Print the name of the type of x. Explain if it is a static type or dynamic type.

This may not make sense in all languages.

Idiom #94 Print type of variable

Print the name of the type of x. Explain if it is a static type or dynamic type.

This may not make sense in all languages.

Code
console.log (typeof x)
console.log (x == null ? x + '' : x.constructor.name)
Code
console.log (typeof x);
Comments bubble
The first one is the "simple" one—in most cases you'll get `object` unless you put in a primitive or function.
The second one gives you the name of the function used to build x—it always works due to the "everything is an object" principle.
Comments bubble
In most cases you'll get `object` unless you put in a primitive or function.