Multiple Variables Holding Data - Which has the Highest Value in JavaScript?


To fetch the highest value, use the Math.max() from JavaScript. Since, we want the one with maximum value, use the Object.values.

Example

const studentMarksDetails=
{
   marks1:78,
   marks2:69,
   marks3:79,
   marks4:74
}
const maximumMarks = Math.max(...Object.values(studentMarksDetails));
console.log("The highest marks="+maximumMarks);
for (const key of Object.keys(studentMarksDetails)){
   if (studentMarksDetails[key] == maximumMarks) {
      console.log(`The Object='${key}'`);
      break;
   }
}

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo91.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo91.js
The highest marks=79
The Object='marks3'

Updated on: 07-Sep-2020

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements