- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Comparing integers by taking two numbers in JavaScript
We are required to write a JavaScript function that takes in two numbers, let’s say num1 and num2.
If num1 is greater than num2, our function should return greater.
If num2 is greater than num1, our function should return smaller.
Otherwise, the function should return equal.
Example
Following is the code −
const compareIntegers = (num1, num2) => { if(typeof num1 !== 'number' || typeof num2 !== 'number'){ return false; }; if(num1 > num2){ return 'greater'; }else if(num2 > num1){ return 'smaller'; }else{ return 'equal'; }; }; console.log(compareIntegers(12, 56)); console.log(compareIntegers(72, 56)); console.log(compareIntegers(12, 12)); console.log(compareIntegers(12, 33));
Output
Following is the output on console −
smaller greater equal smaller
- Related Articles
- Taking part from array of numbers by percent JavaScript
- Maximum Product of Two Numbers in a List of Integers in JavaScript
- Comparing corresponding values of two arrays in JavaScript
- Armstrong Numbers between two integers?
- Taking the absolute sum of Array of Numbers in JavaScript
- Comparing forEach() and reduce() for summing an array of numbers in JavaScript.
- Comparing two strings in MySQL?
- Comparing two dates in PHP
- Comparing two strings in C++
- JavaScript algorithm for converting integers to roman numbers
- Random whole number between two integers JavaScript
- Remove values in an array by comparing the items 0th index in JavaScript?
- Comparing and filling arrays in JavaScript
- JavaScript program to convert positive integers to roman numbers
- Maximum products of two integers in linear time in JavaScript

Advertisements