Skip to main content
Ben Nadel at the New York ColdFusion User Group (Feb. 2009) with: Clark Valberg and Joakim Marner
Ben Nadel at the New York ColdFusion User Group (Feb. 2009) with: Clark Valberg ( @clarkvalberg ) Joakim Marner

ColdFusion 2016 Skips Over Undefined Elements With For-In Array Loop

Published in Comments (5)

This morning, when I was writing up my post on generating a Java Thread Dump programmatically in ColdFusion, one thing I had to cope with was an Array that may or may not contain undefined elements. I wanted to use a for-in loop to iterate over the given array; but, I wasn't sure how ColdFusion would behave if the given array was "sparse". As such, I used CommandBox's multi-engine support to quickly test the behavior and see how and if it has changed over time.

First, I put together a test script so that I could see how many iterations ColdFusion performed when an array contained undefined elements:


	// Create an array in which several indices are "undefined".
	values = [];
	arrayResize( values, 5 );
	values[ 1 ] = "defined";
	values[ 5 ] = "defined";

	// Check to see which ColdFusion engine we're running on.
	if ( structKeyExists( server, "lucee" ) ) {

		writeOutput( "Lucee " & server.lucee.version & " : " );

	} else {

		writeOutput( "ColdFusion " & server.coldfusion.productVersion & " : " );


	// Iterate over the values in the array and indicate iteration.
	for ( value in values ) {

		writeOutput( " x " );



As you can see, 3-of-the-5 elements in this array will be undefined. Now, when I execute this code in the various ColdFusion engines (using CommandBox), I get the following output (concatenated):

ColdFusion 10,0,23,302580 : x x x x x

ColdFusion 11,0,15,311399 : x x x x x

ColdFusion 2016,0,07,311392 : x x

ColdFusion 2018,0,01,311402 : x x

Lucee : x x x x x

As you can see, ColdFusion 2016 and ColdFusion 2018 only perform two iterations, skipping over the undefined values. Earlier versions of Adobe ColdFusion and even the latest version of Lucee ColdFusion all include undefined elements during the for-in array iteration.

NOTE: You can use the isNull() function to determine if the iteration index value is undefined.

Once I figured out where the behavior changed, I looked up the ColdFusion 2016 release notes and saw that the old behavior was filed as a bug by the master-blaster, Adam Cameron. I am not sure that I entirely agree with the idea that the old behavior was a bug. And, it's interesting to see that Lucee does not offer compatible behavior. That said, skipping over undefined elements would certainly make most use-cases easier.

Anyway, this was mostly a note-to-self so that I could mentally catalog the change in behavior. Unfortunately, I'm still on an earlier version of ColdFusion at work; so, this change in behavior is somewhat moot. Still, it's good to know about.

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

Reader Comments


I am not sure that I entirely agree with the idea that the old behavior was a bug

it's a bug in that the behaviour of the tag version and script version should match. It doesn't.

I was not making any observation of which of the two behaviours I consider "right" (although now we're talking about it... <cfloop> was doing it right, and that's how should work too).

Keep up the good work, Ben.




Oh snap, I didn't even realize that the Tag version worked differently! Thank you for clarifying on that. I am mostly script these days. And, frankly, the use of undefined array elements almost never comes up for me (I just happened to be pulling something back from Java).


Hi Ben,


Hi Ben and Adam,

I've filed CF-4205055 to bring Lucee's consistent looping of sparse arrays to CF.


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