Skip to content

Instantly share code, notes, and snippets.



Created May 14, 2021
What would you like to do?
Using The Elvis / Null Coalescing Operator To Loop Past Array Boundaries In Lucee CFML
letters = [ "A", "B", "C", "D", "E" ];
linkedLetters =
( letter, i ) => {
current: letter,
// We can use the null-coalescing operator to safely wrap around the
// bounds of an array. When the incremented / decremented index results
// in an undefined array item, it means that we've hit a boundary
// condition and we need to fallback to the item at the opposite end of
// the array (either the first or last item depending on the direction).
nextLetter: ( letters[ i + 1 ] ?: letters.first() ),
prevLetter: ( letters[ i - 1 ] ?: letters.last() )
dump( linkedLetters );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment