The Javascript typeof Operator
Page content

What Are Javascript Data Types?

Data means information and in programming it’s often important to know what type of information is being stored in a variable. Data in Javascript can be a number, a string, an object, undefined, and boolean (true or false). Specific times and cases when you might need to verify Javascript data types will definitely come up if you continue to program in this scripting language. This is why you’ll want to know how to use the Javascript typeof operator.

How to Use the Javascript typeof Operator

The syntax for the Javascript typeof operator is: typeof (operand). While it isn’t absolutely necessary to enclose the operand within parenthesis, it’s considered good practice and clean code. Remember that the operand could be a variable, an object, a string, a special keyword such as null, or even an object property. You can discover or confirm an operator’s data type quickly and easily using the Javascript typeof operator. Let’s take a look at the simple script I’ve written to demonstrate this.

Practice with the Javascript typeof Operator

You should already be familiar with basic (X)HTML and with declaring and initializing variables. I’ve embedded this short script in an HTML document as evidenced by the HTML tags. Please note that because this isn’t a live script, I didn’t bother to declare a document type, as I’m not using the strict or transitional DTD because I’ve no concern about validation. Nevertheless, I’ve enclosed the script within a character data (CDATA) section out of habit. All live web pages that I author are validated; so, if I need to embed Javascript in them, I avoid validation errors by enclosing the script within a CDATA section.

I’ve declared variables: aNum, aWord, response, and noThing. I’ve also created and initialized a Date object (var thisDay = new Date()). You can see that the variable aNum is a number, aWord is a string, thisDay is an object (the Date object), response is boolean, and noThing is not assigned a value. There are a few things to which you should pay special attention when working with the Javascript typeof operator. When I first wrote the small script above, I accidentally enclosed the value assigned to the variable response in quote marks. Doing this turned the value true into a string instead of the boolean type I wanted. The same will happen if you inadvertently enclose a number in quote marks. Also, keep in mind that any variable to which you haven’t assigned a value will result in an undefined data type if you perform the Javascript typeof operator on it.

If I hadn’t created the Date object before attempting to perform the Javascript typeof operator on it, I would have received a runtime error. You might want to take a look at your variables sections before attempting to confirm their Javascript data types. One more note to keep in mind is that if you use the Javascript typeof operator on the keyword null or on a variable with a null value, the data type will be seen as an object. This is a little strange; but, it’s the way things are.

In the print section of the script (document.write) you can see the use of the Javascript typeof operator on variables and the Date object I have. The results are confirmed and printed for me when the .htm file is opened by a web browser as seen if you click on the screen shot below. Don’t be confused by the fact that Number() and String() are also functions.