D3.js | d3.sum() function Last Updated : 26 Jun, 2019 Comments Improve Suggest changes Like Article Like Report The d3.sum() function in D3.js is used to return the sum of the given array's elements. If the array is empty then it returns 0. Syntax: d3.sum(Array) Parameters: This function accepts a parameters Array which is an array of elements whose sum are to be calculated. Return Value: It returns the sum of the given array's element. Below programs illustrate the d3.sum() function in D3.js. Example 1: javascript <html> <head> <title> Getting sum of the elements of given array </title> </head> <body> <script src='https://d3js.org/d3.v4.min.js'> </script> <script> // initialising the array of elements var Array1 = [10, 20, 30, 40, 50, 60]; var Array2 = [1, 2]; var Array3 = [0, 1.5, 6.8]; var Array4 = [.8, .08, .008]; // Calling to d3.sum() function A = d3.sum(Array1); B = d3.sum(Array2); C = d3.sum(Array3); D = d3.sum(Array4); // Getting sum of the given array's element document.write(A + "<br>"); document.write(B + "<br>"); document.write(C + "<br>"); document.write(D + "<br>"); </script> </body> </html> Output: 210 3 8.3 0.888 Example 2: javascript <html> <head> <title> Getting sum of the elements of given array </title> </head> <body> <script src='https://d3js.org/d3.v4.min.js'> </script> <script> // initialising the array of elements var Array1 = []; var Array2 = ["a", "b", "c"]; var Array3 = [1, "B", "C"]; var Array4 = ["Geek", "Geeks", 2, 3, "GeeksforGeeks"]; // Calling to d3.sum() function A = d3.sum(Array1); B = d3.sum(Array2); C = d3.sum(Array3); D = d3.sum(Array4); // Getting sum of the given array's element document.write(A + "<br>"); document.write(B + "<br>"); document.write(C + "<br>"); document.write(D + "<br>"); </script> </body> </html> Output: 0 0 1 5 Note: In the above output, if the parameter is empty or strings then it returns 0 and if the parameter is string including some integers value then the sum of the integer's value is returned. Ref: https://devdocs.io/d3~4/d3-array#sum Comment More infoAdvertise with us Next Article D3.js d3.max() function K Kanchan_Ray Follow Improve Article Tags : JavaScript Web Technologies D3.js Similar Reads D3.js | d3.min() function The d3.min() function in D3.js is used to returns the minimum value in the given array using natural order. If an array is empty then it returns undefined as output. Syntax: d3.min(Array) Parameters: This function accepts a parameters Array which is an array of elements whose minimum value is to be 2 min read D3.js d3.max() function The d3.max() function in D3.js is used to return the maximum value in the given array using natural order. If an array is empty then it returns undefined as output. Syntax:d3.max(Array)Parameters:This function accepts parameters Array which is an array of elements whose maximum value is to be calcul 2 min read D3.js d3.mean() function The d3.mean() function in D3.js is used to return the mean or average of the given array's elements. If the array is empty then it returns undefined. Syntax:d3.mean(Array)Parameters:This function accepts a parameter Array which is an array of elements whose mean are to be calculated. Here elements s 2 min read D3.js | d3.keys() Function The d3.keys() function in D3.js is used to return an array containing the property names or keys of the specified object or an associative array. Syntax: d3.keys(object) Parameters: This function accepts single parameter object containing key and value in pairs. Return Value: It returns the keys of 1 min read D3.js | d3.scan() function The d3.scan() function is a built-in function in D3.js which scans the array linearly and returns the index of the minimum element according to the specified comparator. The function returns undefined when there are no comparable elements in the array. Syntax: d3.scan(array, comparator) Parameters: 2 min read D3.js | d3.pairs() function The d3.pairs() function in D3.js is used to create pair of array elements from the given array elements. If the given array is having less than two elements then it returns the empty array. Syntax: d3.pairs(Array) Parameters: This function accepts a parameter Array whose elements are going to be pai 2 min read Like