JavaScript get the type of variable | typeof or instanceof method example

Use typeof method to get a type of variable in JavaScript. But typeof is only good for returning the “primitive” types such as number, boolean, object, string, and symbols. You can also use instanceof to test if an object is of a specific type.

Example of JavaScript get the type of variable

Use typeof:

HTML example get the type of variables in JavaScript.

Output:

JavaScript get the type of variable

Console code .

Another interesting way is to examine the output of Object.prototype.toString:

The typeof method is only good for returning the “primitive” types such as number, boolean, object, string, and symbols. You can also use instanceof to test if an object is of a specific type.

Do comment if you have any doubts and suggestions on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.