This section explains the syntax of the CFScript language.
You enclose CFScript regions inside <cfscript> and </cfscript> tags. No other CFML tags are allowed inside a cfscript region. The following lines show a minimal script:
<cfscript> a = 2; </cfscript>
CFScript variables can be of any ColdFusion type, such as numbers, strings, arrays, queries, and objects. The CFScript code can read and write any variables that are available in the page that contains the script. This includes all common scope variables, such as session, application, and server variables.
CFScript supports all CFML expressions. CFML expressions include operators (such as +, -, EQ, and so on), as well as all CFML functions.
You can use several comparison operators in CFScript only, not in CFML tags. (You can also use the corresponding CFML operators in CFScript.) The following table lists the CFScript-only operators and the equivalent operator that you can use in CFML tags or CFScript:
CFScript operator |
CFML operator |
CFScript operator |
CFML operator |
---|---|---|---|
== |
EQ |
!= |
NEQ |
< |
LT |
<= |
LTE |
> |
GT |
>= |
GTE |
For information about CFML expressions, operators, and functions, see Using Expressions and Number Signs.
CFScript supports the following statements:
assignment |
for-in |
try-catch |
function call |
while |
function (function definition) |
if-else |
do-while |
var (in custom functions only) |
switch-case-default |
break |
return (in custom functions only) |
for |
continue |
|
The following rules apply to statements:
Curly brace characters ({ and }) group multiple CFScript statements together so that they are treated as a single unit or statement. This enables you to create code blocks in conditional statements, such as the following:
if(score GT 0) { result = "positive"; Positives = Positives + 1; }
In this example, both assignment statements are executed if the score is greater than 0. If they were not in the code block, only the first line would execute.
You do not have to put brace characters on their own lines in the code. For example, you could put the open brace in the preceding example on the same line as the if statement, and some programmers use this style. However, putting at least the ending brace on its own line makes it easier to read the code and separate out code blocks.
CFScript has two forms of comments: single line and multiline.
A single line comment begins with two forward slashes (//) and ends at the line end; for example:
//This is a single-line comment. //This is a second single-line comment.
A multiline comment starts with a /* marker and continues until it reaches a */ marker; for example:
/*This is a multiline comment. You do not need to start each line with a comment indicator. This is the last line in the comment. */
The following rules apply to comments:
In addition to the names of ColdFusion functions and words reserved by ColdFusion expressions (such as NOT, AND, IS, and so on), the following words are reserved in CFScript. Do not use these words as variables or identifiers in your scripting code:
break |
default |
function |
switch |
case |
do |
if |
try |
catch |
else |
in |
var |
continue |
for |
return |
while |
Although CFScript and JavaScript are similar, they have several key differences. The following list identifies CFScript features that differ from JavaScript:
You cannot include ColdFusion tags in CFScript. However, you can include cfscript blocks inside other ColdFusion tags, such as cfoutput.
Tag |
CFScript equivalent |
---|---|
Direct assignment, such as Myvar=1; |
|
WriteOutput function |
|
if and else statements |
|
switch, case, and default statements |
|
Indexed cfloop |
for loops |
Conditional cfloop |
while loops and do while loops |
Structure cfloop |
for in loop. (There is no equivalent for queries, lists, or objects.) |
break statement. CFScript also has a continue statement that has no equivalent CFML tag. |
|
try and catch statements |
|
Direct assignment of Cookie scope memory-only variables. You cannot use direct assignment to set persistent cookies that are stored on the user's system. |
|
CreateObject function |
For example, the following example loops through a query in CFScript:
... <cfscript> // Loop through the qGetEmails RecordSet for (x = 1; x LTE qGetEmails.RecordCount; x=x+1) { This_id = qGetEmails.Emails_id[x]; This_Subject = qGetEmails.Subject[x]; This_RecFrom = qGetEmails.RecFrom[x]; This_SentTo = qGetEmails.SentTo[x]; This_dReceived = qGetEmails.dReceived[x]; This_Body = qGetEmails.Body[x]; ... // More code goes here. } </cfscript>