JavaScript Program for Division of Two Numbers
Last Updated :
31 Aug, 2023
In this article, we will see the basic approaches for the division of two numbers in JavaScript. The division is used in many languages, In JavaScript it can be done using some operators or some other methods.
Javascript has no data type such as integer, float, or double so we can directly use them via letting variables directly.
Example:
Let's take two numbers :
a = 10
b = 5
here , a is dividend and b is divisor
a / b = 10 / 5 = 2
These are the following ways by which we can divide two numbers:
- Using Division / Operator
- Using Functions
- Using the Arrow Function
- Using Division Assignment
Using Division / Operator
In this approach we divide two numbers in JavaScript involves using the / operator to perform arithmetic division on numeric variables, resulting in their division.
Syntax:
a / b
Example: This example describes the division of 2 numbers using the / operator.
Javascript
let a = 20;
let b = 10;
console.log(a/b);
console.log(b/a);
let c = a/b;
console.log(c);
console.log(20/10);
console.log(10/20);
|
Using Functions
In this approach we are creating a custom function and passing parameters to them, dividing those parametres and returning the result.
Example: This example describes the subtraction of 2 numbers using Function.
Javascript
let x = 40;
let y = 20;
function divide(x, y) {
return x / y;
}
console.log( "After division : " + divide(x, y));
|
Output
After division : 2
Using Arrow function
We can make our custom Arrow function for division of numbers in which we can pass parameters and divide them accordingly and returns out the result of that division. It will work same as normal function but syntatically it differs from it.
Example: This example describes the subtraction of 2 numbers using Arrow function.
Javascript
let x = 40 ;
let y = 20 ;
let ans = (x,y) => x/y;
console.log( "After division : " + ans(x,y));
|
Output
After division : 2
Using Division Assignment Operator
This “/=” operator is used for division where we divide the variable’s value present in the left side to the value or variable present in the right side and that result get stored in left variable itself.
Syntax:
leftOperand /= rightOperand;
Example: This example decribes division of two numbers using Division assignment operator
Javascript
let x = 40;
x /= 10.0;
console.log(x);
|
Share your thoughts in the comments
Please Login to comment...