ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software

javascript typeof operator

Updated on November 2, 2007

JavaScript typeof operator tells you what type of data you're dealing with:

var BooleanValue = true; 

var NumericalValue = 354;

var StringValue = "This is a String";

alert(typeof BooleanValue) // displays "boolean"

alert(typeof NumericalValue) // displays "number"

alert(typeof StringValue) // displays "string"


    0 of 8192 characters used
    Post Comment

    • profile image

      3 years ago


    • profile image

      Johne288 3 years ago

      You are my inspiration, I possess few web logs and rarely run out from to brand. dbebkacdbgea

    • profile image

      Gaksorast 5 years ago

      Прелестный блог, делюсь с Вами, наслаждайтесь, изучайте, зарабатывайте,

    • profile image

      Wisen Technologies 5 years ago

      The typeof operator returns “object” for a value that is null. This was actually an error in the original JavaScript implementation that was then copied in ECMAScript. Today, it is rationalized that null is considered a placeholder for an object, even though, technically, it is a primitive value.


    • profile image

      Dave Stewart 5 years ago

      Just had to join in.

      @Zoroastro There are times when you can say "this is" in coding, but using the typeof operator is not one of them. If you had a point, it got well and truly lost in your incoherent, misleading, opinionated and fairly inaccurate rant.

      I won't bother to knock down each of your arguments as I'm busy, my role here is to highlight to others who may think you know what you're talking about, not to get sucked in.

    • profile image

      David 6 years ago

      @Zoroastro: That's outrageous. What about a javascript model setter that needs to check for associated user-defined hooks (functions) that may or may not be present for a given attribute? You need to determine the type to avoid executing the function in cases where it is absent.

    • profile image

      Tim 6 years ago

      @Zoroastro Or... here's a thought, use the === operator to check value AND type.

    • profile image

      Neo Invoice 6 years ago

      It is very useful when you don't have 100% control of the environment. Done.

    • profile image

      Chris 7 years ago

      Zoroastro has demonstrated fairly well that he doesn't understand much about JavaScript at all. For instance, he says that EVERY object in JavaScript is typeof "object". That is simply not true. What about a function?

      var func = Function();

      "object" == typeof func; // false

      func instanceof Object; // true

      Since I have effectively knocked down the central argument of all that gobbledygook, that every JS object is type "object", I'll stop there.

      typeof should be avoided for other reasons:

    • profile image

      Alexander 7 years ago

      /* Formatted on 2011/01/03 16:17 (Formatter Plus v4.8.7) */

      SELECT a.matter_ref_number, a.reminder_activity, a.reminder_date,

      a.reminder_to, b.claim_type_id,





      ) AS CLAIM_Name


      WHERE a.matter_ref_number = b.matter_ref_number

    • profile image

      shortsman 7 years ago

      Just wanted to add my 2 cents:


    • profile image

      pantsman 7 years ago

      well hello..

    • profile image

      nisar 7 years ago


    • profile image

      Daniel Earwicker 7 years ago


      "javascript isn't OOP" - true. It's way better than OOP!

      Also you're confusing OOP and static type safety. Smalltalk is a hugely influential OO language, but like JS it is dynamically-typed: a variable may store anything.

      The need to check the type of an object is not automatically a sign of poor design. Yes, often the operation to be performed depends on the type of a single object, and so by calling a method on the object we can avoid an explicit "type-switch": just call the method and let the object decide how to implement it.

      But it is also often true that the operation to be performed depends on the types of two or more objects. Traditional OO (as exemplified by Java and C++) has no answer to this problem. e.g. a "window" receives an "event". The operation to perform depends on both the type of the window and the type of the event. If you make a "handleEvent" method on the window, you need a type-switch to examine the type of event.

      OO is not quite the ultimate solution to all programming problems that you may have been told! :)

    • profile image

      Jim 7 years ago

      Daniel completely agree, surely typeof is essential when relying on parameters passed in by an unknown user.

    • profile image

      Daniel Cotter 7 years ago

      Zoroastro -- What about the case in which you want to know whether a passed-in parameter is of the right type for the code that follows? This especially applies in the case of something like a jQuery plugin, where oftentimes the end-user of the plugin can pass in custom (and quite possibly wrong) settings.

    • profile image

      David 7 years ago

      since this is the #1 google result for javascript typeof, it should probably include a reasonable list of javascript types, such as "object", "undefined", "function"

      men you are honest!! this is because you are rated #1 ;)

    • profile image

      fredmc 7 years ago

      Zoroastro, you said "A variable should NEVER change it's type... if it was a string at the beginning of the program, it should be a string at the end of the program."

      How about when I want to increment a counter on a page? I read the existing value with innerHTML or get the value of a form field. Either gives me a string. If I don't change the type of the variable to a number, I concatenate, rather than add.

    • profile image

      Zoroastro 7 years ago

      typeof is almost never needed in good OOP. The times it is "needed" can usually be avoided by using a simple flag, and the remaining times almost always are a result of poor encapsulation and piss-poor programming ability.

      The thing is, javascript isn't OOP, it's actually not OBP as some people believe. Javascript is a type insensitive language that has few rules on object malleability. That means that you can change almost any aspect of any "new Object()" anywhere in the program. You can do OOP with this, but the type insensitivity upon which it relies means that any object will always be 'type' Object.

      Now, recall that I said that good OOP tries to avoid the need of typeof? Well, I didn't explain that the reason people use typeof is because they want to know what their OBJECT's type is.

      Now wait a min, Javascript's objects are all type Object! So you're saying you don't know the TYPE of a VARIABLE that YOU defined (unlike objects, which are defined by the program).

      Stop using overloaded functions. Javascript does NOT support function overloading, Javascript ONLY supports "argument insensitive" functions (meaning you can leave some arguments off and check if they are undefined (NOT that they are false *:| ("0" == false) && (0 == false) && ([0] == false)

      Three very good reasons to use (0 == undefined) (for arguments sake, null is the value that undefined holds... so either or).

      Javascript has type insensitive functions so you can pass OBJECTS that have the same INTERFACE without hassle, not so you can avoid writing an additional function.

      Stop redefining namespaces (variables)! A variable should NEVER change it's type... if it was a string at the beginning of the program, it should be a string at the end of the program.

      The type insensitivity of variables exists mostly so functions can pass fail flags around (such as returning a -1) instead of returning null.

      Note, however, that even if you pass a fail flag to a variable that should hold a string, you are not changing the variables type (other than from being undefined). By checking if the fail flag is there before using it as a string, you inherently confirm that the object is, indeed, a string.

    • profile image

      Daniel Earwicker 7 years ago

      Google: FAIL.

    • profile image

      talhah 8 years ago

      i agree with cory, such a waste of time

    • profile image

      Robin 8 years ago

      The following site gives more details of the operator of "typeof":

    • profile image

      Wingi 8 years ago

      Please read the 4 articles of Matthias Reuter finding and converting objects in javascript instead the "try and error method"!!!

    • profile image

      Cory Marsh 8 years ago

      since this is the #1 google result for javascript typeof, it should probably include a reasonable list of javascript types, such as "object", "undefined", "function"

      <a href=" Javascript Doc</a>

    • profile image

      mirko 8 years ago

      a fungeje to aj na objekty?

    • profile image

      i9 9 years ago