Logo

Programming-Idioms

History of Idiom 94 > diff from v35 to v36

Edit summary for version 36 by 1.7.4:
New JS implementation by user [1.7.4]

Version 35

2018-10-15, 16:48:34

Version 36

2019-01-23, 19:25:57

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)
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.