Using JavaScript Arithmetic Operators in ColdFusion 8

by Simon. Average Reading Time: about 3 minutes.

An often mentioned complaint by ColdFusion developers is the lack of operators commonly found in other programming languages such as JavaScript. For example, instead of the greater-than (>) symbol we have been restricted to the more wordy GT or GREATER THAN operator. However, in ColdFusion 8 this has changed and we have more freedom to use familiar JavaScript operators in <cfscript> blocks.

In the following series of posts, I will introduce the changes and show some simple examples. The first in the series is Arithmetic Operators.

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). These haven’t changed from ColdFusion version 7 to 8.

A key change comes with the use of Unary Operators. The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

These operators work as they do in most other programming languages.

% (Modulus)

The modulus operator is used as follows:

var1 % var2

The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. For example, 12 % 5 returns 2.

In ColdFusion this is written:

<cfif myQuery.currentRow % 2></cfif>

A good use of the modulus operator would be to alternate row colours in tabular data. The above code could be wrapped around a CSS class, dynamically changing the name of the class on each row iteration.

++ (Increment)

The increment operator is used as follows:

var++ or ++var

This operator increments (adds one to) its operand and returns a value. If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing. If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.

For example, if x is 10, then the statement y = x++ sets y to 10 and increments x to 11. If x is 10, then the statement y = ++x increments x to 11 and sets y to 11.

In ColdFusion, a simple example:

<cfset x = 10 />
<cfset y = x++ />
<cfdump var="#variables#" label="Increment Operator Test 1" />

The above code returns:

Increment Operator Test 1

Switching the order of the operator to be before the variable:

<cfset x = 10 />
<cfset y = ++x />
<cfdump var="#variables#" label="Increment Operator Test 2" />

The above code returns; note that both variables return 11:

Increment Operator Test 2

-- (Decrement)

The decrement operator is used as follows:

var-- or --var

This operator decrements (subtracts one from) its operand and returns a value. If used postfix (for example, x–), then it returns the value before decrementing. If used prefix (for example, –x), then it returns the value after decrementing.

For example, if x is three, then the statement y = x– sets y to 3 and decrements x to 2. If x is 3, then the statement y = –x decrements x to 2 and sets y to 2.

In ColdFusion, a slightly more complex example:

//decrement the variable y
for(y=5; y > 0; y--)
//returns 54321

The above code loops from 5 to 1, decrementing the value of y by 1 on each iteration. The returned output is therefore 54321.

We could now combine the increment and decrement operators to do even more complex operations. E.g.:

//create a new array
arrTemp = arrayNew(1);
//create a new variable for the array counter
x = 1;
//loop y, decrement the variable y
for(y=5; y > 0; y--)
	arrTemp[x++] = y;
<cfdump var="#arrTemp#" label="Combined Increment Decrement Operator Test" />

The above code loops from 5 to 1, decrementing the value of y by 1 on each iteration. At the same time, the value of x increments by 1 on each iteration and is used to define the array index, i.e. arrTemp[1].

The returned output can be displayed as follows:

Combined Increment Decrement Operator Test

Such code serves to demonstrate how you can set up complex operations without the need to use the following, antiquated operations:

<cfset x = incrementValue(x) />
<cfset y = y+1 />
<cfset a = decrementValue(a) />
<cfset b = b-1 />

– (Unary Negation)

The unary negation operator precedes its operand and negates it. For example, y = -x negates the value of x and assigns that to y; that is, if x were 3, y would get the value -3 and x would retain the value 3. This is not new to ColdFusion 8, but it is worth a mention.

In ColdFusion, this can be shown by the following:

<cfset x = 3 />
<cfset y = -x />
<cfdump var="#variables#" label="Unary Operator Test" />

The above code returns:

Unary Operator Test

This article has been tagged

, , , , , , , , , , , , , ,

Other articles I recommend

Using JavaScript Assignment Operators in ColdFusion 8

In my first post in this series, I introduced Using JavaScript Arithmetic Operators in ColdFusion 8. Now we’ll concentrate on Assignment operators.

An assignment operator assigns a value to its left operand based on the value of its right operand.

The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x. The other assignment operators are usually shorthand for standard operations, as shown in the following table.

Using JavaScript Logical Operators in ColdFusion 8

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

Operator Precedence in ColdFusion

In arithmetic and algebra, when a number or expression is both preceded and followed by a binary operation (a calculation involving two operands), a rule is required for which operation should be applied first. From the earliest use of mathematical notation, multiplication took precedence over addition, whichever side of a number it appeared on. Thus 3 + 4 — 5 = 5 — 4 + 3 = 23.

  • gowrikumar

    i want to use the cold fusion cfset value into javascript funtion and javascript function value is ued in to cold fusion

  • http://www.simonwhatley.co.uk Simon

    @gowrikumar I think you are confusing the two programming languages. ColdFusion is server-side and JavaScript is generally client-side.

    1. If you want to pass a value generated by ColdFusion to JavaScript, ColdFusion needs to output the value to the page whereupon JavaScript can be used to read the value. Typically you may put the value into a formfield, give the field an ID, which can then be referenced by JavaScript.

    2. If you want to use a value generated by JavaScript, you could write it into a form field and submit the form. ColdFusion can then process the submitted data.

    Alternatively, JavaScript can make an AJAX request to a ColdFusion webservice, passing JavaScript generated values and or values read from the web-page using JavaScript.

    3. … I won’t mention the third way as it’s a nasty hack and should be avoided at all costs. Needless to say it relates to dynamically rendering the JavaScript with ColdFusion.