SAT / ACT Prep Online Guides and Tips

What Is the JavaScript TypeOf Function? How Does It Work?


We’ve all stumbled on a web page that just doesn’t work. You know the type: the links aren’t clickable, the scrolling is all over the place, and sometimes...they automatically play music. (The worst.)

When a website misbehaves, bad JavaScript is usually at fault. That’s because JavaScript is the scripting language that controls the interactive elements of a website. In fact, JavaScript is actually a robust, powerful scripting language that automates processes like animating images or providing autofill suggestions. Basically, JavaScript is one of the fundamental coding languages of the web.

Like any language, there’s lots you can do with JavaScript. But today, we’re going talk about one specific type of JavaScript command: the TypeOf function. In this article, you’ll learn about the JavaScript TypeOf function, including what it is, how it works, and its most common uses.

So let’s dive in!



What Is a JavaScript Data Type?

Before we start talking about the JavaScript TypeOf function, we first have to talk about JavaScript data types. A data type is a classification given to different kinds of data used in programming. There are seven data types that can be used in JavaScript, and we’ve broken them down in the table below:

Data Type Use
Number Deals with any number, including decimals. You can also do operations (like multiplication, division, etc.) with this data type.
String A string of characters surrounded by single or double quotes.
Boolean Sets a true/false (or yes/no) value.
Null Indicates the intentional absence of a value. Basically means “nothing,” “empty,” or “value unknown.”  
Undefined Also indicates the absence of a value. Unlike “null,” “undefined” is usually used when a variable is declared but a value is unassigned.
Symbols Unique identifiers for objects.
Object Collections of related data. Unlike other data types, which can only contain a single thing, objects store collections of data.





Why Are Data Types Important?

In JavaScript, a data type is basically a category of data that can be stored and/or manipulated within a program. Depending on the type of data you’re using, JavaScript will treat the data differently. So for example, JavaScript deals with an undefined data set much differently than it deals with an object!

The other reason data types are important is because they dictate what you can do with the data you provide—and whether that data will work or not. For example, if you try to assign a value to a “null” data type, it won’t work because null indicates an empty value! Likewise, there are some data types that only do one thing, like the boolean data type, while others can work with collections of data.

In other words, data types are one of the fundamental building blocks of JavaScript.




What Is the Javascript TypeOf Function?

Now that you know what data types are, the typeof function will make more sense: that’s because the JavaScript TypeOf command is used to check the data type of a particular operand, which is a JavaScript array, data type, or object.

In order to use the TypeOf command, you would write it like you would any operation that uses a single value (also known as a unary operator). That means the script would look like this:

typeof x

You can also use the TypeOf operator like you would a JavaScript function. The syntax would look something like this:  


Using the JavaScript TypeOf function is particularly useful for checking the value of JavaScript expressions!


How Do You Use the Javascript TypeOf Function?

When you use the JavaScript TypeOf function to check the data type of an operand, it returns a string, or text, that contains the data type! Here’s an example of how you might check to see if an object is undefined:

If (typeof object === ‘undefined’)

The TypeOf function is an important tool when dealing with complex code. It allows a programmer to quickly check a variable’s data type—or whether it’s “undefined” or “null”—without going through the code line by line! Additionally, the TypeOf function can also check whether an operand is an object or not.

Depending on the output of the TypeOf function, a programmer can adjust their previous code or quickly pick up where they left off on a project. In other words, the TypeOf function is a quick way to check data to make sure that a script will work.


What Are the Possible Outputs of the TypeOf Function?

Because the JavaScript TypeOf function is used to check data types, the possible outputs of the TypeOf operator are all data types, too. Here’s a quick summary of the different type of outputs you might see:

Value TypeOf
undefined “undefined”
null “object”
Boolean (true or false) “boolean”
all numbers “number”
all strings “string”
all symbols “symbol”
all functions “function”
all arrays “object”
native objects “object”
host objects Dependent on implementation
other objects “object”



What’s Next?

If you’re interested in coding, it might be worth considering a computer science major. But how do you know if computer science is really right for you? Here are five key factors to consider when choosing your major.

If you do want to go into computer science, it’s important to get into a good college. One of the best ways to do that is to make sure you’re taking the right courses in high school. Check out this expert guide to the classes you should—and shouldn’t—take before you start your college applications.

You might have heard that AP classes look good on your college application. If you’re not sure what AP classes are or how they affect your college admissions chances, take a look at this AP overview.

Have friends who also need help with test prep? Share this article!

author image
Ashley Robinson
About the Author

Ashley Sufflé Robinson has a Ph.D. in 19th Century English Literature. As a content writer for PrepScholar, Ashley is passionate about giving college-bound students the in-depth information they need to get into the school of their dreams.

Get Free Guides to Boost Your SAT/ACT
100% Privacy. No spam ever.

Ask a Question Below

Have any questions about this article or other topics? Ask below and we'll reply!