Friday, September 5, 2025
HomeLanguagesJavascriptD3.js | d3.extent() function

D3.js | d3.extent() function

The d3.extent() function in D3.js is used to returns the minimum and maximum value in an array from the given array using natural order. If an array is empty then it returns undefined, undefined as output.

Syntax:

d3.extent(Array)

Parameters: This function accepts a parameters Array which is an array of elements whose minimum and maximum value in an array are to be calculated. Here elements might be integers or any strings.

Return Value: It returns the minimum and maximum value in an array from the given array.

Below programs illustrate the d3.extent() function in D3.js.

Example 1:




<html>
  
<head>
    <title>
      Getting minimum and 
      maximum value in an 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.extent() function
        A = d3.extent(Array1);
        B = d3.extent(Array2);
        C = d3.extent(Array3);
        D = d3.extent(Array4);
  
        // Getting minimum and maximum value
        document.write(A + "<br>");
        document.write(B + "<br>");
        document.write(C + "<br>");
        document.write(D + "<br>");
    </script>
</body>
  
</html>


Output:

10, 60
1, 2
0, 6.8
0.008, 0.8

Example 2:




<html>
  
<head>
    <title>
      Getting minimum 
      and maximum value
  </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 = ["A", "B", "C"];
        var Array4 = ["Geek", "Geeks", "neveropen"];
  
        // Calling to d3.extent() function
        A = d3.extent(Array1);
        B = d3.extent(Array2);
        C = d3.extent(Array3);
        D = d3.extent(Array4);
  
        // Getting minimum and maximum value
        document.write(A + "<br>");
        document.write(B + "<br>");
        document.write(C + "<br>");
        document.write(D + "<br>");
    </script>
</body>
  
</html>


Output:

undefined, undefined
a, c
A, C
Geek, neveropen

Note: In the above output, first element is minimum value and second element is maximum value.

Ref: https://devdocs.io/d3~4/d3-array#extent

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS