CFScript


CFScript is an extension of CFML on the ColdFusion platform. CFScript resembles JavaScript. Some ColdFusion developers prefer it since it has less visual and typographical overhead than ordinary CFML.

Usage

It is considered best practice to write ColdFusion Components and all business logic in CFScript and to use CFML only in.cfm files amongst HTML.
Unless it's within a pure script-based ColdFusion Component, all CFScript code must be contained within a CFScript tag pair as follows:


xParam = 115;
yParam = 200;
color = 'FFCC99';


A simple example of a function:


function Sum


A simple example of a component in CFScript, containing two functions:

component

ColdFusion 11, Railo 4.1+, and Lucee 4.5+ both do their best to fully support cf tags in CFScript.
While there may not be direct substitutions for all tags, it is often still possible to achieve the results of a tag in script, but via a different syntax. For example, this is how to get a query into a variable in CFSCRIPT without writing a UDF:


qGetData = new Query;
qGetData.setDataSource;
qGetData.setSQL;
qDateResult = qGetData.Execute.getResult;

Syntax

Since ColdFusion 8, CFScript has supported syntax abbreviations that are common in many other programming languages, such as "++", "<=" and "+=".

Arithmetic operators

Comments

CFScript has two forms of comments: single line and multiline.

// This is a single-line comment.
// This is a second single-line comment.


/* This is a multiline comment.
You do not need to start each line with a comment indicator.
This line is the last line in the comment. */

Try / Catch


try catch finally

Switch statement


switch

Looping

For Loop


for

FOR IN Loop


struct = StructNew;
struct.one = "1";
struct.two = "2";
for
//OUTPUTS onetwo

While Loop


x = 0;
while
// Outputs: 12345

Do / While Loop


x = 0;
do while ;
// Outputs: 1

Looping over an Array


for

Differences from JavaScript

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