View comments | RSS feed

The CFScript language

This section explains the syntax of the CFScript language.

Identifying CFScript

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>

Variables

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.

Expressions

CFScript supports all CFML expressions. CFML expressions include operators (such as +, -, EQ, and so on), as well as all CFML functions. As in all ColdFusion expressions, you must use CFML operators, such as LT, GT, and EQ. You cannot use JavaScript operators, such as <, >, ==, or ++.

For information about CFML expressions, operators, and functions, see Chapter 4, "Using Expressions and Pound Signs".

Statements

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
break
return (in custom functions only)
for
continue

The following rules apply to statements:

Note:   This chapter documents all statements except var and return. For information on these statements, see "Defining functions in CFScript," in Chapter9.

Statement blocks

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.

Comments

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:

Reserved words

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

Differences from JavaScript

Although CFScript and JavaScript are similar, they have several key differences. The following list identifies CFScript features that differ from JavaScript:

CFScript limitation

You cannot include ColdFusion tags in CFScript. However, you can include cfscript blocks inside other ColdFusion tags, such as cfoutput.

CFScript functional equivalents to ColdFusion tags

Although you cannot use ColdFusion tags in CFSCript, CFSCript and ColdFusion functions provide equivalents to several commonly-used CFML tags. The following table lists ColdFusion tags with equivalent functions or CFScript statements:
Tag
CFScript equivalent
cfset
Direct assignment, such as Myvar=1;
cfoutput
WriteOutput function
cfif, cfelseif, cfelse
if and else statements
cfswitch, cfcase, cfdefaultcase
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.)
cfbreak
break statement. CFScript also has a continue statement that has no equivalent CFML tag.
cftry, cfcatch
try and catch statements
cfcookie
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.
cfobject
CreateObject function

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


srhcfmx said on May 31, 2002 at 3:56 PM :
MINOR TYPO:

In the final table, cfscript functional equivalents, the line for structure cfloop:

"Structure cfloop | for in loop. )There is no equivalent for queries, lists, or objects.) "

The close parenthesis before There should be an open parenthesis.
(There is no....
sundvor said on Aug 29, 2002 at 6:23 AM :
I've discovered that yoy _can_ use CFML tags in CFSCRIPT. It's a bit of a trick though, but once set up it's a beeeeuty. :)

1. ##### In /mycustomtags (a cf mapping)/myapp/cfmltags.cfc, create the following:
<cffunction name="cfdump" hint="cfdumps content; author jorgen smith">
<cfargument name="var">
<cfdump var="#arguments.var#">
</cffunction>

2. ##### Back to your source file, do this:

<cfscript>
cUtil=createObject("component","mycustomtags.myapp.cfmltags");
stDemoStruct = structnew(1);
stDemoStruct.printthis = "Hello world";
cUtil.cfdump(stDemoStruct);
</cfscript>


</cfscript>

---

Cheers,
Jørgen - sundvor@hotmail.com
a440guy said on Jan 6, 2003 at 7:44 PM :
This page (The CFSCRIPT Language) does not say anything about operators. I am specifically looking for the string concatenation operator because the ampersand (&) does not seem to work.
No screen name said on Aug 21, 2003 at 5:14 PM :
It *appears* that using createobject - will leave the object in memory for the next time you use it.
So, if you init a String object with "test" - it will have that value next time you call that code - even if you init it with another string value.
Makes sense, but isn't documented.

cheers
-Mike
nnielsen said on Sep 24, 2004 at 6:43 AM :
Just letting the previous poster that the ampersand (&) char does work for string concatenation.

ex:
myString = "";
myString = myString & "hello";
myString = myString & " world";
writeOuput(myString & "!!");

will write to the screen "hello world!!" (not including quotes of course)
No screen name said on Dec 22, 2004 at 7:26 PM :
This also works:

*****************************
<cffunction name="cfquery">
<cfargument name="sql">
<cfquery name="rs" datasource="trailways">
#sql#
</cfquery>
<cfreturn rs>
</cffunction>
***********************************
mysorian said on Aug 11, 2005 at 12:10 PM :
Thw switch conditional seems to require an end switch. However, it makes no difference to the interpreter. Usage of end swith appears to be optional.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/Developing_ColdFusion_MX_Applications_with_CFML/CFScript3.htm