Checking to see if a property exists within an object. - Javascript

This is a discussion on Checking to see if a property exists within an object. - Javascript ; Hi everyone. I am passing data through JSON objects between the server and client via Ajax. I would like to know if it's possible to check for the existence of a property within that object without using try and catch ...

+ Reply to Thread
Results 1 to 4 of 4

Checking to see if a property exists within an object.

  1. Default Checking to see if a property exists within an object.

    Hi everyone.

    I am passing data through JSON objects between the server and client
    via Ajax. I would like to know if it's possible to check for the
    existence of a property within that object without using try and catch
    blocks?

    I was hoping that there might be something else that will just return
    null if the property doesn't exist. If there's not, then it's not a
    problem. I can easily make a function that will check to see if the
    object exists, and pass back null if it doesn't. I just wanted to save
    on unnecessary code if I could.

    Many thanks in advance.

    Daz.


  2. Default Re: Checking to see if a property exists within an object.

    Daz wrote:

    > I am passing data through JSON objects between the server and client
    > via Ajax. I would like to know if it's possible to check for the
    > existence of a property within that object without using try and catch
    > blocks?


    "propertyName" in object
    is one way. Or
    typeof object.propertyName != 'undefined'

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

  3. Default Re: Checking to see if a property exists within an object.

    On May 4, 11:15 am, Daz <cutenfu...@> wrote:
    > Hi everyone.
    >
    > I am passing data through JSON objects between the server and client
    > via Ajax. I would like to know if it's possible to check for the
    > existence of a property within that object without using try and catch
    > blocks?



    Yes. Use in operator :
    Boolean='property' in object

    Example:
    obj={myprop1:222,myprop2}
    if('myprop3' in obj) // < false
    alert("Yes, myprop3 exists in object obj")

    var Var="myprop2"
    if(Var in obj) // < true
    alert("Yes, myprop2 exists in object obj")


  4. Default Re: Checking to see if a property exists within an object.

    On May 4, 6:21 pm, Martin Honnen <mahotr...@yahoo.de> wrote:
    > Daz wrote:
    > > I am passing data through JSON objects between the server and client
    > > via Ajax. I would like to know if it's possible to check for the
    > > existence of a property within that object without using try and catch
    > > blocks?

    >
    > "propertyName" in object
    > is one way. Or
    > typeof object.propertyName != 'undefined'
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


    Wonderful. Thanks a lot Martin.


+ Reply to Thread

Similar Threads

  1. checking if file exists in user's home directory
    By Application Development in forum Fortran
    Replies: 12
    Last Post: 12-07-2007, 09:02 AM
  2. Checking Get variable exists
    By Application Development in forum PHP
    Replies: 2
    Last Post: 09-17-2007, 11:17 AM
  3. Programmatically creating email, checking if exists
    By Application Development in forum ADO DAO RDO RDS
    Replies: 1
    Last Post: 09-02-2007, 10:52 AM
  4. Checking if the App.Config file exists
    By Application Development in forum DOTNET
    Replies: 1
    Last Post: 07-27-2007, 01:11 AM
  5. Checking if URL exists
    By Application Development in forum Javascript
    Replies: 5
    Last Post: 07-14-2007, 09:52 AM