Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Js

Idiom #94 Print the type of a 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.

console.log(typeof x);

In most cases you'll get "object" unless you put in a primitive or function.
console.log (x == null ? x + '' : x.constructor.name);

Gives you the name of the function used to build x—it always works due to the "everything is an object" principle.
(type x)

New implementation...
< >
programming-idioms.org