# The CFScript Language

This section explains the syntax of the CFScript language.

## Statements

Note that in CFScript, semicolons define the end of a statement. Line breaks in your source are insignificant. You can enclose multiple statements in curly braces:

    { statement; statement; statement; }

The following statements are supported in CFScript:

Assignment: lval = expr ;

Note that lval can be a simple variable, an array reference, or a member of a structure.

    x = "positive"; /y = x; a[3]=5;/ structure.member=10;

CFML expression: expr ;

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

For more information on ColdFusion expressions see the CFML Language Reference .

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 ;

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

- a single assignment expression, for example, x=5 or loop=loop+1
- any ColdFusion expression, for example, SetVariable("a",a+1)
- empty

The test-expr can be one of the following:

- any ColdFusion expression, for example, A LT 5, loop LE x, or Y EQ "not found" AND loop LT end
- empty

Here are some examples of for loops:

    // Multiline for statement
    for(Loop1=1;
        Loop1 LT 10;
        Loop1 = Loop1 + 1);
        a[loop1]=loop1;

    // Complete for loop in a single line.
    for(loop=0; loop LT 10; loop=loop+1)arr[loop]=loop;

    // Uses braces to note the code 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);
    while (loop1 LT 10)
    {
     a[loop1] = loop1 + 5;
     loop1 = loop1 + 1;
    }

    a = ArrayNew(1);
    while (loop1 LT 10)
    {
        a[loop1] = loop1 + 5;
        loop1 = loop1 +1;
    }

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

    // Complete do-while loop on a single line
    a = ArrayNew(1);
    do {a[loop1] = loop1 + 5; loop1 = loop1 + 1;} while (loop1 LT 10);

    // Multiline do-while loop
    a = ArrayNew(1);
    do
    {
      a[loop1] = loop1 + 5;
      loop1 = loop1 + 1;
    }
    while (loop1 LT 10);

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

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

No two constants may 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 ;

Note that 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]=0) continue;
        a[loop]=1;
    }

break: break out of the current switch statement or loop

    for( ; ; )
    {
        indx=indx+1;
        if(Find("key",strings[indx],1))
            break;
    }

## Expressions

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

See the CFML Language Reference for information about CFML operators and functions.

Note | You cannot use CFML tags in CFScript.

## 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.

## 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.

## Differences from JavaScript

While CFScript is based on JavaScript, there are some key differences you'll want to note:

- CFScript uses ColdFusion expressions, which are neither a subset nor a superset of JavaScript expressions. For example, there is no < operator in CFScript.
- No user-defined functions or variable declarations are available.
- CFScript is case-insensitive.
- All statements end in a semi-colon, and line breaks in your code are insignificant.
- In CFScript, assignments are statements, not expressions.
- Some implicit objects are not available, such as Window and Document.

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

## 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