Whatterz


Using JavaScript Assignment Operators in ColdFusion 8

by Simon. Average Reading Time: less than a minute.

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.

Operator Shorthand operator Meaning
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y

In unusual situations, the assignment operator is not identical to the Meaning expression in the above table. When the left operand of an assignment operator itself contains an assignment operator, the left operand is evaluated only once. For example:

<cfscript>
a[i++] += 5 //i is evaluated only once
a[i++] = a[i++] + 5 //i is evaluated twice
</cfscript>

This article has been tagged

, , , , , , , , , , ,

Other articles I recommend

Using JavaScript Comparison Operators in ColdFusion 8

Following on from the first three parts of the “Using JavaScript Operators” in ColdFusion 8, where I introduced Arithmetic, Assignment and Logical Operators, it is now the turn of Comparison Operators.

Using JavaScript String Operators in ColdFusion 8

In addition to the comparison operators, which can be used on string values, the concatenation operator (&) concatenates two string values together, returning another string that is the union of the two operand strings. For example, “my ” & “string” returns the string “my string”.

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.

  • http://jobsearchlog.com Michael Brennan-White

    Simon,

    Thanks for this article. It really helps to explains some things to my simple mind.

    Although it isn’t arithmetic the &= operator for appending strings has saved me a lot of time lately.

    Keep up the good work

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

    It is interesting you should mention the String operator &= as I have a small article about it at:

    http://www.simonwhatley.co.uk/using-javascript-string-operators-in-coldfusion-8