CFScript
Unless it's within a pure script-based ColdFusion Component, all CFScript code must be contained within a CFScript tag pair as follows:
<syntaxhighlight lang="cfm">
<cfscript>
 xParam = 115;
 yParam = 200;
 color = 'FFCC99';
</cfscript>
</syntaxhighlight>
A simple example of a function:
<syntaxhighlight lang="cfm">
<cfscript>
function Sum(a, b) {
 var sum = a + b;
 return sum;
</cfscript>
</syntaxhighlight>
A simple example of a component in CFScript, containing two functions:
<syntaxhighlight lang="javascript">
component {
 public void function foo() {
 WriteOutput("Method foo() called");
 public function getString() {
 var x = "hello";
 return x;
</syntaxhighlight>
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:
<syntaxhighlight lang="cfm">
<cfscript>
 qGetData = new Query();
 qGetData .setDataSource('#APPLICATION.datasource#');
 qGetData .setSQL('SELECT column1, column2 FROM table WHERE 1');
 qDateResult = qGetData .Execute().getResult();
</cfscript>
</syntaxhighlight> Since ColdFusion 8, CFScript has supported syntax abbreviations that are common in many other programming languages, such as "++", "<=" and "+=". CFScript has two forms of comments: single line and multiline. <syntaxhighlight lang="javascript">
// This is a single-line comment. // This is a second single-line comment. </syntaxhighlight>
<syntaxhighlight lang="javascript">
/
 You do not need to start each line with a comment indicator. This line is the last line in the comment. </syntaxhighlight> <syntaxhighlight lang="javascript">
try {
 throw(message="Oops", detail="xyz");
} catch (any e) {
 WriteOutput("Error: " & e.message);
 rethrow;
} finally {
 WriteOutput("I run even if no error");
</syntaxhighlight> <syntaxhighlight lang="javascript">
switch (car) {
 case "Nissan":
 WriteOutput("I own a Nissan");
 break;
 case "Toyota":
 WriteOutput("I own a Toyota");
 break;
 default:
 WriteOutput("I'm exotic");
</syntaxhighlight> <syntaxhighlight lang="javascript">
for (i=1; i LTE ArrayLen(array); i=i+1) {
 WriteOutput(array[i]);
</syntaxhighlight> <syntaxhighlight lang="javascript">
struct = StructNew();
struct.one = "1";
struct.two = "2";
for (key in struct) {
 WriteOutput(key);
//OUTPUTS onetwo
</syntaxhighlight> <syntaxhighlight lang="javascript">
x = 0;
while (x LT 5) {
 x = x + 1;
 WriteOutput(x);
// Outputs: 12345
</syntaxhighlight> <syntaxhighlight lang="javascript">
x = 0;
do {
 x = x+1;
 WriteOutput(x);
} while (x LTE 0);
// Outputs: 1
</syntaxhighlight> <syntaxhighlight lang="javascript">
for (item in array) {
 doSomething(item);
</syntaxhighlight> Although CFScript and JavaScript are similar, they have several key differences. The following list identifies CFScript features that differ from JavaScript:
Usage
CFScript resembles JavaScript.