Question on a typeof

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Question on a typeof

    [code=JAVASCRIPT]
    if(typeof obj_name){}
    if(typeof obj_name!=undef ined){}
    [/code]

    What is the difference between these two?
    Basically undefined returns false ....if i want to check whether a object exists or not can't i do that simply using
    [CODE=JAVASCRIPT]if(obj_ref){}[?CODE]
    Could you explain ?
  • Logician
    New Member
    • Feb 2007
    • 210

    #2
    Originally posted by dmjpro
    [code=JAVASCRIPT]
    if(typeof obj_name){}[/code]
    typeof returns the type as a string, so the above always evaluates true
    Code:
    if(typeof obj_name!=undefined){}
    The above always evaluates true because undefined isn't a string.

    Code:
    if(typeof myObj != "undefined")
     .......
    Also you can test by naming the object as a child; so if myObj is global
    Code:
    if( window.myObj != undefined )
     .......

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by Logician
      typeof returns the type as a string, so the above always evaluates true
      The above always evaluates true because undefined isn't a string.

      Code:
      if(typeof myObj != "undefined")
       .......
      Also you can test by naming the object as a child; so if myObj is global
      Code:
      if( window.myObj != undefined )
       .......
      Yeah Now I got it ....

      Comment

      Working...