Skip to content

How to find type of variable in JavaScript?

In this tutorial, let’s learn how to identify the type of a variable in JavaScript.

In short, the answer in typeof. In JavaScript, the typeof operator returns the data type of its operand in the form of a string. The operand can be any object, function, or variable. 

console.log(typeof "foo"); //string

console.log(typeof true); //boolean

console.log(typeof 17); //number

console.log(typeof undefined); //undefined

console.log(typeof justAnUndeclaredVariable); //undefined

console.log(typeof {name: "Anand"}); //object

console.log(typeof function() {} ); //function
See also  How to get a key in a JavaScript object by its value?
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

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