JavaScript TypeOf: Uses & Examples

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

When writing JavaScript, it may be necessary to determine what type of object you are working with. This lesson will cover the typeof function, providing working code examples.

JavaScript Typeof

Unlike Java, JavaScript lets you create variables without necessarily specifying the TYPE of variable. In other words, you can create a payRate variable and give it text values. When you declare variables in JavaScript, all you need is the name of the variable and an optional value.

The following code declares a few JavaScript variables.

JavaScript typeof variable declaration

By looking at the variable names alone, we could probably guess each type (number or string). The values give it away. At least, in this case, we are confident of the type, except for checkSum. We'll get to that in a little bit.

What if you don't know? JavaScript is so universal in web applications that there are countless scripts that developers provide to each other. Corporate websites may have dozens of JavaScript files that are shared across hundreds of web pages. Perhaps all you know is that the function displayEmployeeData has three variables. But what type are they, so you know how to handle them? Or what if the object(s) don't exist at all?

For this, we have the typeof method, which returns the type of a given variable based on its current value. When checking for the value typeof returns, always enclose the value in single quotes (such as 'number').

Variable Type TypeOf Will Display...
Number number
String string
Undefined undefined
Boolean boolean
Function function
Objects object
Null object

(The fact that Null returns the keyword object is an interesting feature of JavaScript, going back to the first versions of the language. It doesn't make a lot of sense. It is technically a bug in the core of JavaScript, but it's so embedded that it will never be fixed.)

Now that we have an assessment of what typeof will return, let's use typeof to display the variable types for the previous variables we declared. To do so, we can add a series of statements to simply display the type. You could also update existing form fields, write to the document, or even send the result to another function.

The following is a long alert statement (used so we only get one output box). However, pay close attention to how the typeof keyword is used in the statement.

Javascript typeof alerts

When the web page processes, the following alerts are displayed.

Javascript typeof alerts output

Uses for Typeof

One use for typeof is to check and see if an object or variable is defined. Remember the checkSum variable from above? Because the variable is undefined, typeof will return undefined, since it has no value.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account