KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Checking if a property does not exist in an object (null vs. undefined)
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: October 21, 2024

There are a few ways to check if a property exists in an object. One way is compare it to the null value, for example:

var $myObj : Object

$myObj:={A: 40; B: 25}

If ($myObj.C=Null)
   $myObj.C:=0
Else
   $myObj.C+=1
End if

Another way is to use the command Undefined, for example:

var $myObj : Object

$myObj:={A: 40; B: 25}

If (Undefined($myObj.C))
   $myObj.C:=0
Else
   $myObj.C+=1
End if

Similarly, the command OB is defined can also be used, for example:

var $myObj : Object

$myObj:={A: 40; B: 25}

If (OB Is defined($myObj; "C")=False)
   $myObj.C:=0
Else
   $myObj.C+=1
End if

Note that an object property can also be assigned the null value, so comparison with null value could actually give a false positive when checking if the property does not exist. For this reason, using Undefined or OB is defined is a bit more precise.