- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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'
Advertisements