CFScript

CFML programming language extension for ColdFusion


title: "CFScript" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["cfml-programming-language", "scripting-languages"] description: "CFML programming language extension for ColdFusion" topic_path: "technology/programming-languages" source: "https://en.wikipedia.org/wiki/CFScript" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary CFML programming language extension for ColdFusion ::

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

Unless it is within a pure script-based ColdFusion Component, all CFScript code must be contained within a CFScript tag pair as follows: ::code[lang=cfm] xParam = 115; yParam = 200; color = 'FFCC99'; ::

A simple example of a function: ::code[lang=cfm] function Sum(a, b) { var sum = a + b; return sum; } ::

A simple example of a component in CFScript, containing two functions: ::code[lang=javascript] component { public void function foo() { WriteOutput("Method foo() called
"); }

public function getString() {
    var x = "hello";
    return x;
}

} ::

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: ::code[lang=cfm] qGetData = new Query(); qGetData.setDataSource('#Application.datasource#'); qGetData.setSQL('SELECT column1, column2 FROM table WHERE 1'); qDateResult = qGetData.Execute().getResult(); ::

Syntax

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

Arithmetic operators

::data[format=table]

OperatorDescription
+ - * /Basic arithmetic: Addition, subtraction, multiplication, and division.
++ --Increment and decrement. Increase or decrease the variable by one.
+= -= *= /= %=Compound assignment operators. The variable on the right is used as both an element in the expression and the result variable. Thus, the expression a += b is equivalent to a = a +b.
+ -Unary arithmetic: Set the sign of a number.
MOD or %Modulus: Return the remainder after a number is divided by a divisor. The result has the same sign as the divisor. The value to the right of the operator should be an integer; using a non-numeric value causes an error, and if you specify a real number, ColdFusion ignores the fractional part (for example, 11 MOD 4.7 is 3).
\Integer division: Divide an integer by another integer. The result is also an integer; for example, 9\4 is 2. The right operand cannot be zero
^Exponentiation: Return the result of a number raised to a power (exponent). Use the caret character (^) to separate the number from the power; for example, 2^3 is 8. Real and negative numbers are allowed for both the base and the exponent. However, any expression that equates to an imaginary number, such -1^.5 results in the string "-1.#IND. ColdFusion does not support imaginary or complex numbers.
::

Comments

CFScript has two forms of comments: single line and multiline. ::code[lang=javascript] // This is a single-line comment. // This is a second single-line comment. ::

::code[lang=javascript] /* 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

::code[lang=javascript] try { throw(message="Oops", detail="xyz"); } catch (any e) { WriteOutput("Error: " & e.message); rethrow; } finally { WriteOutput("I run even if no error"); } ::

Switch statement

::code[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"); } ::

Looping

For loop

::code[lang=javascript] for (i=1; i <= ArrayLen(array); i=i+1) { WriteOutput(array[i]); } ::

FOR IN Loop

::code[lang=javascript] struct = StructNew(); struct.one = "1"; struct.two = "2"; for (key in struct) { WriteOutput(key); } //OUTPUTS onetwo ::

While loop

::code[lang=javascript] x = 0; while (x < 5) { x = x + 1; WriteOutput(x); } // Outputs: 12345 ::

Do / while loop

::code[lang=javascript] x = 0; do { x = x + 1; WriteOutput(x); } while (x <= 0); // Outputs: 1 ::

Looping over an array

::code[lang=javascript] for (item in array) { doSomething(item); } ::

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 uses ColdFusion expression, which are not a superset or a subset of JavaScript expressions. In particular, ColdFusion expressions do not support bitwise operators, and the ColdFusion MOD or % operator operates differently from the corresponding JavaScript % operator: In ColdFusion, the operator does integer arithmetic and ignores fractional parts. ColdFusion expressions also support the EQV, IMP, CONTAINS, and DOES NOT CONTAIN operators that are not supported in JavaScript.
  • Variable declarations (var keyword) are only used in user-defined functions and threads.
  • CFScript is not case sensitive.
  • All statements end with a semicolon, and line breaks in the code are ignored.
  • Assignments are statements, not expressions, and therefore cannot be used in situations that require evaluating the assignment operation.
  • JavaScript objects, such as window and document, are not available.
  • Only the ColdFusion server processes CFScript. There is no client-side CFScript.

References

References

  1. Nadel, Ben. (May 31, 2007). "Learning ColdFusion 8: All Hail The New ++ Operator".

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

cfml-programming-languagescripting-languages