Pin Me

JavaScript Typeof: Explanation & Examples

written by: Sam OBrien•edited by: Amber Neely•updated: 9/13/2010

Learn how and when to use the Javascript typeof operator to quickly and easily determine the current data type of any variable.

  • slide 1 of 3

    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.

  • slide 2 of 3

    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.



    <title>Practice with the Javascript typeof Operator </title>



    <script ='text/javascript'>


    var aNum = 40

    var aWord = "Hello"

    var thisDay = new Date()

    var response = true

    var noThing

    document.write("<h3>My variables are: aNum, aWord, thisDay, response, and noThing.</h3>")

    document.write("<p>The data type of aNum is: ", typeof(aNum), "</p>")

    document.write("<p>The data type of aWord is: ", typeof(aWord), "</p>")

    document.write("<p>The data type of thisDay is: ", typeof(thisDay), "</p>")

    document.write("<p>The data type of response is: ", typeof(response), "</p>")

    document.write("<p>The data type of noThing is: ", typeof(noThing), "</p>")





    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.

  • slide 3 of 3