How to Add the Immediate-If Function to an ASP Page

We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 7.0 running on Microsoft Windows Server 2008. IIS 7.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:For more information about IIS 7.0, visit the following Microsoft Web site:


One of the common Visual Basic (VB) or Visual Basic for Applications (VBA) functions that was omitted from VBScript (VBS) for Active Server Pages (ASP) is the "Immediate-If" or IIf() function.

The IIf() function is used in place of If..Then..Else statements, where a single expression is being tested and specific values are returned if the expression evaluates as true or false. Because this command was omitted from the ASP VBS command set, you can use the code below to add this functionality to an ASP page by adding IIf() as a new function to the code.

More Information

The following syntax explanation is from Microsoft's Developer Network:

IIf Function

Returns one of two parts, depending on the evaluation of an expression.



exprRequired. Expression you want to evaluate.
truepartRequired. Value or expression returned if expr is True.
falsepartRequired. Value or expression returned if expr is False.


The IIf() function can be used in ASP code by adding the following function to a page:
Function IIf(i,j,k)
If i Then IIf = j Else IIf = k
End Function


The following ASP code shows an example page using the IIf() function to determine if a number is even or odd:
Function IIf(i,j,k)
If i Then IIf = j Else IIf = k
End Function
<head><title>IIF() example</title></head>
For X = 1 to 5
Response.Write "<li>" & X & " is an " & IIf(X Mod 2, "Odd", "Even") & " number.</li>" & vbCrLf

For more information on Microsoft Scripting Technologies, please see the information at the following URL: