Wednesday, July 3, 2024
HomeLanguagesJavascriptHow to check first number is divisible by second one in JavaScript...

How to check first number is divisible by second one in JavaScript ?

Given two numbers and the task is to check the first number is divisible by the second number or not with the help of JavaScript.

Before getting into the coding part, first let us know about the modulo operator and triple equals. To find a number is divisible by another or not, we simply use the reminder operator and if remainder is 0 then it is divisible otherwise not divisible.

Example:

variable a = 10; 
variable b = 5;

if ((a % b) == 0) {
    // a is divisible by b
else {
    //a is not divisible by b
}

We have used “==” operator for comparing the value of expressions. But when it comes to JavaScript we use “===”(Triple equals).

Reason to use Triple equals: The triple equal sign tests for strict equality between two values. Both the type and the value you’re comparing have to be exactly the same.

Examples of strict equality:

4 === 4
// true (Both numbers, equal values)

'gfg' === 'gfg'
// true (Both Strings, equal values)

54 === '54'
// false (Number compared with String)

The double equal tests for loose equality and performs type coercion. It means we compare two values after converting them to a common type.

Example:

3 == '3'
// true

Hence , we use “===”(Triple equals) in JavaScript rather than “==”(Double equals).

Example: This is the code to check if the first numeric argument is divisible by the second one in JavaScript.

HTML




<!DOCTYPE html>
<html>
  
<body style="text-align:center;">
  
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
    <p>
        Enter first number:
        <input type="text" id="txt1" name="text1"><br>
        <br> Enter second number:
        <input type="text" id="txt2" name="text2"><br>
        <br>
        <button onclick="myFunction()">Divide</button>
    </p>
  
    <p id="demo" style="font-size: 20px; 
        font-weight: bold;">
    </p>
  
    <script>
        function myFunction() {
            var a = Number(document.getElementById("txt1").value);
            var b = Number(document.getElementById("txt2").value);
              
            const isDivisible =
                (dividend, divisor) => dividend % divisor === 0;
            if (a % b === 0) {
                document.getElementById("demo").innerHTML =
                    a + " is divisible by " + b;
            } else {
                document.getElementById("demo").innerHTML =
                    a + " is not divisible by " + b;
            }
        }
    </script>
</body>
  
</html>


Output:

Hence, this is how you can check if the first numeric argument is divisible by the second one or not in JavaScript.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments