What is max() in Dart?

The max() function returns the largest value of two numbers sent as parameters.

The figure below shows a visual representation of the max() function.

Visual representation of the max() function

To use the max() function, you will need to import the dart:math library as shown below:

import dart:math

Syntax

The syntax of the max() function is as follows:

type max(num-1, type num-2)
// type extends number

Parameters

The max() function takes two numbers as parameters.

The numbers can be of type Int, Double, Float, or Long.

Return value

The max() function returns the largest value from the two numbers sent as parameters.

Code

The code below shows how to use max() in Dart.

import 'dart:convert';
import 'dart:math';
void main() {
// two positive numbers
print("The value of max(10, 0) = ${max(10, 0)}");
//one positive and the other negative
print("The value of max(4, -6) = ${max(4, -6)}");
// both negative numbers
print("The value of max(-10, -9) = ${max(-10, -9)}");
// both double numbers
print("The value of max(12.234,1.2345) = ${max(12.234,1.2345)}");
// one int and the other double
print("The value of max(12.234,1) = ${max(12.234,1)}");
}

For every pair of values in the code above, the max() function returns the larger of the two.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources