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:
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
const a = 14;
const b = 2;
console.log(a / b);
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
const a = 123;
const b = 1.7;
console.log(a / b);
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:
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: This example shows the implemetation of the above-explained approach.
JavaScript
const a = 411;
const b = 2;
const r = parseInt(a) / parseInt(b);
console.log(r);
Method 3: Using math library in JavaScript
In this approach, 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.
Example: This example shows the implemetation of the above-explained approach.
JavaScript
let a = 10;
let b = 5;
let q = Math.floor(a / b);
let r = a % b;
console.log('Quotient = ', q, 'Remainder = ', r);
OutputQuotient = 2 Remainder = 0
Note: 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: Using Bitwise Operator
In this approach, 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.
Example: This example shows the implemetation of the above-explained approach.
JavaScript
let a = 14;
let b = 5;
let q = ~~(a / b);
let r = a % b;
console.log('Quotient = ', q, 'Remainder = ', r);
OutputQuotient = 2 Remainder = 4
In this example, we are using the Lodash _.divide() method that return the result of the division of two numbers.
Example: This example shows the implemetation of the above-explained approach.
JavaScript
// Requiring the lodash library
const _ = require("lodash");
// Use of _.divide() method
let gfg = _.divide(1508, 4);
// Printing the output
console.log(gfg)
Output:
377
Similar Reads
Division(/) Arithmetic Operator in JavaScript
JavaScript airthmetic division operator is used to find the quotient of operands. The left operator is treated as a dividend and the right operator is treated as a divisor. Syntax: a/bReturn Type: It returns the quotient of the operands Example 1: In this example, we will perform basic division on N
1 min read
Division Assignment(/=) Operator in JavaScript
JavaScript Division Assignment Operator in JavaScript is represented by "/=". This operator is used to divide the value of the variable by the value of the right operand and that result is going to be assigned to the variable. This can also be explained as dividing the value of the variable by an ex
1 min read
What is Math in JavaScript?
JavaScript provides a built-in Math object that provides a set of methods to perform mathematical operations. These operations range from basic to more complex functions like trigonometry and logarithms. The Math object allows you to perform calculations without the need for writing custom functions
2 min read
JavaScript Math.ceil( ) function
The JavaScript Math.ceil() function rounds a given number up to the nearest integer. It always rounds towards positive infinity, meaning it increases the number to the next whole number if it's not already an integer. This function is useful for rounding up values in calculations.SyntaxMath.ceil(val
2 min read
Long Division Method
Long Division is a technique of dividing numbers, algebraic expressions, and decimals stepwise and sequentially. In this technique the number which is to be divided is called Dividend, the number which divides is called Divisor, the number which we get as a result of division is called a Quotient, a
15+ min read
JavaScript Assignment Operators
Assignment operators are used to assign values to variables in JavaScript.JavaScript// Lets take some variables x = 10 y = 20 x = y ; console.log(x); console.log(y); Output20 20 More Assignment OperatorsThere are so many assignment operators as shown in the table with the description.OPERATOR NAMESH
5 min read
What is positive infinity in JavaScript ?
The positive infinity in Javascript is a number that is constant and represents a value that is the highest available. It can be generated using a self-made function or by an arithmetic operation. Note: JavaScript shows the POSITIVE_INFINITY value as Infinity. Positive Infinity: It is different from
2 min read
JavaScript RangeError: BigInt divison by Zero
In JavaScript, the BigInt type was introduced to represent integers with arbitrary precision. This means that BigInt can represent very large numbers, but it also means that BigInt must follow certain mathematical rules. In particular, division by zero is mathematically undefined and impossible. Att
2 min read
Octal to Decimal conversion using JavaScript
An octal number is a number system that uses a base of 8 and numbers are represented using the digits 0 through 7. Decimal numbers are a base 10 number system which uses 10 digits from 0 to 9. We are given a number a octal number and we have to convert it into decimal in JavaScript and print the res
3 min read
Gaussian/banker's rounding in JavaScript
The task is to implement the Gaussian/banker's rounding with the help of JavaScript. Here, few approaches are discussed. Approach 1: Get the d value(which tells the digits to which we need to round off). Calculate the m(pow(10, d)). Then shift the decimal after the digits we want to round off(if d=2
3 min read