Description

Use this function to calculate the sum of values in an array.

Returns

The sum of values in an array. If the array parameter value is an empty array, returns zero.

Category

Array functionsMathematical functions

Function syntax

ArraySum(array[,ignoreUndefined])

Parameters

Parameter

Description

array

(Required) Name of an array.

ignoreUndefined

(Optional) Boolean (true/false) value denoting whether to ignore empty (Null or "") values in the array while adding the elements.

History

ColdFusion (2016 release) Update 3 - Added ignoreUndefined parameter.

Example

<cfscript>
       myArray=[921,22,133,40,345,58,-6];
       WriteOutput("Sum of values in myArray is:");
       WriteOutput(ArraySum(myArray) & "|"); // returns sum of values in myArray
       myNewArray=[];
       WriteOutput("Sum of values in myNewArray is:");
       WriteOutput(ArraySum(myNewArray)); //returns zero since myNewArray is empty
</cfscript>

Output

Sum of values in myArray is:1513|Sum of values in myNewArray is:0

Example - using the ignoreUndefined parameter

<cfscript>
       myarray=ArrayNew(1);
       myarray=[-23,56,97,javacast("null",""),"",1,23];
       sum=arraysum(myarray,true); //ignoreUndefined=true
       writeoutput(sum);
</cfscript>

Output

154

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy