Skip to main content
Ben Nadel at dev.Objective() 2015 (Bloomington, MN) with: Gabriel Perez
Ben Nadel at dev.Objective() 2015 (Bloomington, MN) with: Gabriel Perez

Multi-Var Assignments In A Single Line In ColdFusion

By
Published in Comments (3)

The other day, when I was looking up some operators for my post on natural language operators in ColdFusion, I saw something in the documentation that surprised me: ColdFusion has the ability to assign multiple Function-local variables in a single line. It's a very strange notation, so I'll probably never use it. But, since it surprised me, I figured there's other people out there who have never seen it.

To be clear, ColdFusion has always allowed multiple values to be assigned at one time. In my onApplicationStart() bootstrapping method, for example, I'll often use assignments like this to both cache a value in the application scope and use it locally for the rest of the method call:

var service = application.service = new Service();

The syntax that I noticed in the ColdFusion documentation was the ability to include multiple var (variable declarations) in the same expression:

var a = var b = "hello";

This is declaring two new local variables, a and b, and is assigning them both to the value, "hello". Notice that each variable has its own var keyword. If b were to omit the var keyword, it would have assigned b to the variables scope, not the local scope (unless b was already declared in the local scope in a previous statement).

This same line can be rewritten to use the explicit local scope:

local.a = local.b = "hello";

Or, we can always combine both syntax approaches:

var a = local.b = "hello";

Nothing here is right or wrong - it all comes down to what you find most readable. Personally, I like to place each var statement on its own line.

To tie this all together, here's a working example:

<cfscript>
	// Immediately invoked function expression (IFFE).
	(() => {

		// Normal VAR assignment.
		var a = "a";
		var z = "z";

		// MULTI VAR assignment. Note that "var" is required for EACH new variable. But,
		// that we can also overwrite exiting variables (z) without the "var" keyword.
		var b = var c = z = "multi";

		// Alternative approach using the explicit LOCAL scope.
		local.m = local.n = "Oh";

		writeDump( local );

	})();
</cfscript>

This works in both Adobe ColdFusion and Lucee CFML; but, I have no idea if this is a recent addition or if this is how its always worked. And, when we run the code above and dump-out the local scope we get the following output:

Output of local scope showing results of variable assignment. Both

This is more of a curiosity to me. I don't love the way it reads to have a var in the middle of my assignments. But, it's good to know that it exists in the CFML language.

Want to use code from this post? Check out the license.

Reader Comments

40 Comments

Truth is, while debugging, my eyes would skip right over that and never see it. Like a missing semicolon. I'm all about clarity in my code, before efficiency, because I'll be debugging it later! 😆

15,848 Comments

@Andrew,

Right, you mean like, var a = 1, b = 2; Yeah, I believe that currently / still throws a syntax error in CFML.

@Will,

I agree - it looks like something is missing. I tend to put all my declarations on separate lines.

Post A Comment — I'd Love To Hear From You!

Post a Comment

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel