Determining variable type

Discussion in 'Javascript' started by mouseit, May 27, 2007.

  1. mouseit

    mouseit Guest

    If I have a variable of some sort, say x, how can I find out what
    variable type javascript thinks it is? For example, if I've declared x
    as 5 earlier, how do I know now whether it's a string or a number,
    isNaN returns false, regardless of whether I declared it as "5" or 5.
     
    mouseit, May 27, 2007
    #1
    1. Advertising

  2. mouseit

    Lee Guest

    mouseit said:
    >
    >If I have a variable of some sort, say x, how can I find out what
    >variable type javascript thinks it is? For example, if I've declared x
    >as 5 earlier, how do I know now whether it's a string or a number,
    >isNaN returns false, regardless of whether I declared it as "5" or 5.


    Javascript doesn't think it needs to know.
    Why do you think you do?


    --
     
    Lee, May 27, 2007
    #2
    1. Advertising

  3. mouseit

    -Lost Guest

    mouseit wrote:
    > If I have a variable of some sort, say x, how can I find out what
    > variable type javascript thinks it is? For example, if I've declared x
    > as 5 earlier, how do I know now whether it's a string or a number,
    > isNaN returns false, regardless of whether I declared it as "5" or 5.


    typeof or instanceof.

    typeof will give the generic object type.
    instanceof will only return true if it is an object of type [Object].
    ([Object] being any valid JavaScript Object; String, Function, Array,
    Object, whatever.)

    --
    -Lost
    Remove the extra words to reply by e-mail. Don't e-mail me. I am
    kidding. No I am not.
     
    -Lost, May 27, 2007
    #3
  4. mouseit

    mouseit Guest

    On May 26, 8:58 pm, Douglas Crockford <> wrote:
    > mouseit wrote:
    > > If I have a variable of some sort, say x, how can I find out what
    > > variable type javascript thinks it is? For example, if I've declared x
    > > as 5 earlier, how do I know now whether it's a string or a number,
    > > isNaN returns false, regardless of whether I declared it as "5" or 5.

    >
    > typeof 5 === 'number'
    > typeof '5' === 'string'
    >
    > http://javascript.crockford.com/survey.html


    Thank you all!
     
    mouseit, May 27, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marshall Dudley

    Determining connection type

    Marshall Dudley, Dec 7, 2004, in forum: Perl
    Replies:
    0
    Views:
    478
    Marshall Dudley
    Dec 7, 2004
  2. =?Utf-8?B?VHJldm9yIEFuZHJldw==?=

    Determining the type of a UserControl (ascx) in code

    =?Utf-8?B?VHJldm9yIEFuZHJldw==?=, Jun 27, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,842
    =?Utf-8?B?VHJldm9yIEFuZHJldw==?=
    Jun 27, 2005
  3. -
    Replies:
    7
    Views:
    468
    Tony Morris
    Feb 11, 2005
  4. Schraalhans Keukenmeester

    Determining Variable type

    Schraalhans Keukenmeester, Aug 5, 2006, in forum: C Programming
    Replies:
    9
    Views:
    325
    Keith Thompson
    Aug 7, 2006
  5. John Reye
    Replies:
    12
    Views:
    681
    Robert Miles
    Jul 24, 2012
Loading...

Share This Page