The CFScript Language

This section explains the syntax of the CFScript language.

Comments

Comments in CFScript blocks begin with two forward slashes (//) and end at the line end. You can also enclose CFScript comments between /* and */. Note that you cannot nest /* and */ inside other comment lines.

Variables

Variables can be of any ColdFusion type, such as numbers, strings, arrays, queries, and COM objects. You can read and write variables within the script region.

Expressions

CFScript supports all CFML expressions. CFML expressions include operators (such as +, -, EQ, and so on), as well as all CFML functions.

For information about CFML expression, operators, and functions, see the CFML Reference.


Note

You cannot use CFML tags in CFScript.


Statements

In CFScript, semicolons define the end of a statement. Line breaks are insignificant. Enclose multiple statements in curly braces to group them for use in an expression:

{ statement; statement; statement; }

The following statements are supported in CFScript:

Assignment: lval = expr ;

lval can be a simple variable, an array reference, or a member of a structure. For example:

x = "positive";

y = x; 

a[3]=5; 

structure.member=10;

CFML expression: expr ;

StructInsert(employee,"lastname",FORM.lastname);

if-else: if(expr) statement [else statement] ;

if(score GT 1)

  result = "positive";

else

  result = "negative";

for loop: for (init-expr ; test-expr ; final-expr) statement ;

init-expr and final-expr can be one of the following:

The test-expr can be one of the following:

Here are some examples of for loops:

for(Loop=1;

  Loop LT 10;

  Loop = Loop + 1)

  a[loop]=loop;





// Complete for loop in a single line.

for(loop=1; loop LT 10; loop=loop+1)a[loop]=loop;



// Use braces to indicate multiple statements to loop over

for( ; ; )

{

  indx=indx+1;

  if(Find("key",strings[indx],1))

    break;

}

while loop: while (expr) statement ;

// Use braces to note the code to loop over

a = ArrayNew(1);

loop = 1;

while (loop LT 10)

{

 a[loop] = loop + 5;

 loop = loop + 1;

}

do-while loop: do statement while (expr) ;

// Complete do-while loop on a single line

a = ArrayNew(1);

loop = 1;

do {a[loop] = loop + 5; loop = loop + 1;} while (loop LT 10);



// Multiline do-while loop

a = ArrayNew(1);

loop = 1;

do

{

  a[loop] = loop + 5;

  loop = loop + 1;

}

while (loop LT 10);

switch-case: switch (expr) {case constant : statement break ; default : statement }

In this syntax, constant must be a constant (that is, not a variable, a function, or other expression). Only one default statement is allowed. You can use multiple case statements. You cannot mix Boolean and numeric case values in a switch statement.

No two constants can be the same inside a switch statement.

switch(name)

{

  case "John":

  {

    male=true;

    found=true;

    break;

  }

  case "Mary":

  {

    male=false;

    found=true;

    break;

  }

  default:

  {

    found=false;

    break;

  }

} //end switch

for-in loop: for (variable in collection) statement ;

variable can be any ColdFusion identifier, and collection must be the name of an existing ColdFusion structure.

for (x in mystruct) mystruct[x]=0;

continue: skip to next loop iteration

for ( loop=1; loop LT 10; loop = loop+1)

{

  if(a[loop] EQ 0) continue;

  a[loop]=1;

}

break: break out of the current switch statement or loop

indx = 0;

for( ; ; )

{

  indx=indx+1;

  if(Find("key",strings[indx],1))

    break;

}

return, var: See "Defining and Using Custom Functions"

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:
for
while
do
if
else
switch
case
break
default
in
continue
function
var
return
   

Differences from JavaScript

CFScript is similar to JavaScript, however, there are some key differences in CFScript:


Note

CFScript is not directly exportable to JavaScript. Only a limited subset of JavaScript can run inside CFScript.




Banner.Novgorod.Ru