coldfusion Variables Checking if a Variable Exists


You can check if a variable has been defined in a scope by using ColdFusion's built in StructKeyExists() function. This can be used inside a <cfif> tag to prevent error messages in the event you attempt to refer to a variable that does not exist. You can also use this function to determine whether a user has performed a certain action or not. The syntax for the function is

StructKeyExists(structure, "key")

The following example checks if the variable firstName exists in the variables scope.

<cfif StructKeyExists(variables, "firstName")>
    Hello #variables.firstname#!
    Hello stranger!

Alternatively, you may use the function:


To avoid ambiguity, it is recommended to declare the scope. For example, If you have a variable in the scope test

<cfset = "Tracy" />

and you test for name in the global scope, you will get a result of true.

isDefined("name") <!--- true --->
isDefined("") <!--- false--->
isDefined("") <!--- true --->