DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Determining Type of a Variable

  1. #1
    Scott Guest

    Determining Type of a Variable


    I am looking for a way to determine whether or not a javascript variable is
    a string or an array. Is there a function or some reasonably simple way
    to do this?

    Here's the problem. I am using WDDX through ColdFusion to convert a database
    query to a javascript variable. The variable, after conversion, is really
    a multi-dimensional array. Using this array, I can dynamically populate
    a series of pull-down menus from the query results fairly easily.

    However, there are cases where the affected select boxes should be left empty.
    This should occur when there are no sub-elements to populate the select
    boxes. However, instead of returning an empty array value in the WDDX2JS
    conversion, what I'm getting is a string in those empty elements. When I
    check for the length of the value - expecting it to be 0 - I'm actually getting
    the length of the string rather than the length of an empty array, which
    is throwing off the logic of the algorithm.

    Is there any way to determine the type of that variable?

  2. #2
    Ajith Edassery Guest

    Re: Determining Type of a Variable


    Use the 'typeof' function in JavaScript. It returns the type of a variable
    at runtime.

    e.g.

    var str1 = "This is a string"
    var arr1 = new Array("This", "is", "a", "string");

    alert(typeof(str1)); // shows 'string' in the message box
    alert(typeof(arr1)); // shows 'object' in the message box

    Hope this helps... Have fun!!!

    Ajith


    "Scott" <sspringer@pipelineinteractive.com> wrote:
    >
    >I am looking for a way to determine whether or not a javascript variable

    is
    >a string or an array. Is there a function or some reasonably simple way
    >to do this?
    >
    >Here's the problem. I am using WDDX through ColdFusion to convert a database
    >query to a javascript variable. The variable, after conversion, is really
    >a multi-dimensional array. Using this array, I can dynamically populate
    >a series of pull-down menus from the query results fairly easily.
    >
    >However, there are cases where the affected select boxes should be left

    empty.
    > This should occur when there are no sub-elements to populate the select
    >boxes. However, instead of returning an empty array value in the WDDX2JS
    >conversion, what I'm getting is a string in those empty elements. When

    I
    >check for the length of the value - expecting it to be 0 - I'm actually

    getting
    >the length of the string rather than the length of an empty array, which
    >is throwing off the logic of the algorithm.
    >
    >Is there any way to determine the type of that variable?



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center