However, as a subset, there are limitations. To begin with, there is nothing that you can do inside CFScript that you can't do with tags. Until ColdFusion 5, CFScript didn't really extend the functionality of ColdFusion. Now it is an important facet of the language because you must use CFScript to write user-defined functions. Furthermore, it makes the language more accessible for programmers accustomed to other scripting languages, and it speeds up your performance in certain situations.
How Does CFScript Work?
In order to write ColdFusion script, you must surround your script code with a ColdFusion tag, <cfscript>. This tag takes no attributes, and the closing tag is required. The chief limitation of CFScript is that you may not use any ColdFusion tags inside a script block. Variables that you set inside a script block are visible just like any other variable in ColdFusion. You can manipulate data inside the script block, and it is visible to the rest of your request.
Setting Variables and Commenting
You set a variable by simply writing the name of the variable that you wish to set, an equals sign, and the value. If the variable does not exist, ColdFusion creates it. Let's set a simple variable in Listing 1 to see how this works.
Listing 1: Setting a Variable
<!-----first declare that you're about to write script by using the <cfscript> tag.------> <!---then create a variable called "drink". Set its value to "coffee"-----> <cfscript> Drink = "coffee"; </cfscript> <!---output the variable value-------> <cfoutput>#drink#</cfoutput>
The variable set inside the block is just the same as if you had created it using <cfset>. The output to the browser is coffee. One thing you probably noticed is the presence of a semicolon at the end of the line. Commands must end in a semicolon within CFScript.
There are two ways to create comments inside a script block. To write a one-line comment, you use two slashes:
<cfscript> // set the drink variable. I am a comment. Drink = "coffee"; </cfscript>
In order to create a multiline comment, you use a /* combination, as you would in SQL or Java, like this:
<cfscript> /* I am a multiline comment */ Drink = "coffee"; </cfscript>
You will not see a performance benefit to using CFScript to set only one or two variables in this manner. However, once you have to set three or more at once, you will see a performance increase. The reason for this is that ColdFusion does not have to read and interpret each successive <cfset> tag one by one. Instead, the entire <cfscript> block gets passed to the ColdFusion server and interpreted at once. So, you're likely to see a performance benefit