Skip to content
Related Articles
Open in App
Not now

Related Articles

Division in JavaScript

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 30 Nov, 2022
Improve Article
Save Article

In every programming language, the operation of division is used often. The division operator (/) is used in JavaScript to divide integers. You may use the division operator or a predefined JavaScript technique that treats every number as an integer to divide two values.

Use the techniques listed below to divide two numbers:

  • Division (/) operator
  • parseInt() method
  • Division Using the Math Library in JavaScript
  • Division Using the Bitwise Operators in JavaScript

Method 1: Division (/) Operator

Use the division operator, represented in JavaScript by the symbol (/), to divide two integers. Two operands may be divided; the divided operand is known as the “dividend,” while the dividing operand is referred to as the “divisor.” The “quotient” is the value that remains after division. Follow the given-provided syntax for division:

dividend/divisor;

Example 1: In this example, we will use an integer dividend with an integer divisor. Assigning integer values to the two integers “a” and “b,” we shall divide them in this example. Then, when “b” is a divisor, use the console.log() function with “a” as a dividend:

Javascript




<script>
    const a = 14;
    const b = 2;
    console.log(a/b);
</script>


Output:

7

Example 2: In this example, we will use an integer dividend with a float divisor. Now, we’ll divide the integer value by the float value, where “a” is equal to “123” and “b” is equal to “1.7”:

Javascript




<script>
    const a = 123;
    const b = 1.7;
    console.log(a/b);
</script>


Output:

72.3529411764706

Example 3: The division operator will be used in the following example to divide the integer “3” by the floating point number “125.75”.

Javascript




<script>
    const a = 125.75;
    const b = 3;
    console.log(a/b);
</script>


Output:

41.916666666666664

Example 4: In this example, we will use float dividend with float divisor. The variables that, respectively, hold the float values “15.75” and “5.5” are as follows.

Javascript




<script>
    const a = 15.75;
    const b = 5.5;
    console.log(a/b);
</script>


Output:

2.636363636363638

Method 2: JavaScript parseInt() Method

A JavaScript predefined function called “parseInt()” accepts values in string format and converts them to integer formats. For instance, it will return “20” if you offer it the floating-point number “20.87” as a parameter. When using parseInt() to divide two integers, the method initially delivers the result in integer format before doing the division operation.

Syntax: Use the following syntax when using the parseInt() function to divide two numbers:

parseInt(a)/parseInt(b); 

In this case, the “parseInt()” function divides the values by the division operator after taking values in either integer or decimal format and returning them in integer format.

Example 1: Assigning the integer values “414” and “2” to the two integers “a” and “b” will divide them in this example. Then, use the division operator to call the parseInt() function, and save the output in the newly formed variable “r”. In this case, parseInt() returns the same values since it accepts an integer value. When we divide them, depending on the result, it either produces an integer value or a decimal number. Then, using the “console.log()” function, print the value of “r”:
Because the dividend is an odd integer number and the dividend is an even integer number, the result is “205.5”, which is the decimal value:

Javascript




<script>
    const a = 411;
    const b = 2;
    const r = parseInt(a)/parseInt(b);
    console.log(r);
</script>


Output:

205.5

Method 3: In this example, we are using division using the math library in javaScript. JavaScript makes it simple to divide two variables, and the result is in floating-point integers. However, the Math library, which offers a variety of functions, may be used to get the division’s quotient and remainder. For instance, the Math.floor() or Math.trunc() function, which transforms the floating-point value to an integer, may be used to get the quotient of a division, and the % character can be used to obtain the remainder. Find the remainder and quotient of 10 divided by 5, for instance. Check out the code below.

Javascript




<script>
    var a = 10;
    var b = 5;
    var q = Math.floor(a/b);
    var r = a%b;
    console.log('Quotient = ',q,'Remainder = ',r);
</script>


Output:

"Quotient = "
2
"Remainder = "
0

Explanation: If the numbers are too big, the math library will fail. As an alternative to the Math.floor() method, you may alternatively use the Math.trunc() function, which can handle larger integers. Negative values will result in a failure of the Math.floor() function but not a failure of Math.trunc().

Method 4: In this example, we will be using the Bitwise Operators. The bitwise operators in JavaScript allow us to get the quotient and remainder of a division. For instance, using the bitwise NOT or bitwise OR |0, which transforms the floating-point value to an integer, we may get the quotient of a division. We may also use the% character to retrieve the remaining value. Find the remainder and quotient of 14 divided by 5, for instance. Check out the code below.

Javascript




<script>
    var a = 14;
    var b = 5;
    var q = ~~(a/b);
    var r = a%b;
    console.log('Quotient = ',q,'Remainder = ',r);
</script>


Output:

"Quotient = "
2
"Remainder = "
4

Explanation: As you can see, the result is the same as with the procedure described before. When compared to the Math library, bitwise operators perform better, although they are less capable of handling huge quantities. Therefore, you may utilize the bitwise operators if your numbers are tiny; otherwise, use the Math library. Negative values may also be handled using the bitwise operators. The parseInt() method may also be used to convert a floating-point value to an integer. If you don’t want to utilize any functions, you may use the remaining operator% in a simple calculation like the one below.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!