Open In App

JavaScript Program for Division of Two Numbers

Last Updated : 31 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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; 
  
// Divide directlty 
console.log(a/b); // 2
console.log(b/a); // 0.5
  
// Divide by using another variable
let c = a/b; 
console.log(c); // 2
  
// Divide without any variable
console.log(20/10); // 2
console.log(10/20); // 0.5


Output

2
0.5
2
2
0.5

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




// First variable
let x = 40;
  
// Second variable
let y = 20;
  
// Funciton for dividing the variable
function divide(x, y) {
  return x / y;
}
  
// For printing the result
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




// First variable
let x = 40 ;
  
// Second variable
let y = 20 ; 
  
// Arrow funciton for division
let ans = (x,y) => x/y;
  
// For printing the result in console
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




// Creating a variable having value 40
let x = 40;
  
// Using subtract-and-operator
x /= 10.0;
  
// Printing the result
console.log(x); // 4


Output

4


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads