| by Arround The Web | No comments

How to Divide Two Numbers in JavaScript

Division is a common arithmetic operation in every programming language. In JavaScript, a division operator (/) is utilized for dividing numbers. For dividing two numbers in JavaScript, you can use the division operator or a JavaScript predefined method that considers every number as an integer.

This tutorial will illustrate the methods for dividing two numbers in JavaScript.

How to Divide Two Numbers in JavaScript?

For dividing two numbers, use the below-mentioned methods:

  • Division (/) operator
  • parseInt() method

Let’s see the working of both of them!

Method 1: Divide Two Numbers Using Division (/) Operator

For dividing two numbers in JavaScript, use the division operator that is denoted as (/). You can divide two operands; the operand that is divided is referred to as the “dividend”, while the operand that divides is known as the “divisor”. The resultant value after division is called the “quotient”.

Syntax
Follow the given-provided syntax for division:

dividend/divisor;

Here, the “/” operator will divide the dividend with the divisor.

Example 1: Integer Dividend with Integer Divisor
In this example, we will divide the two numbers “a” and “b” by assigning integer values:

const a = 12;
const b = 2;

Then, call the console.log() method by passing “a” as a dividend while “b” is a divisor:

console.log(a/b);

The output gives “6” by dividing “12/2”:

Example 2: Integer Dividend with Float Divisor
We will now divide the integer value with the float value where the value of the variable “a” is “111” and “b” is “1.6”:

const a = 111;
const b = 1.6;

Print the value after dividing them using the “console.log()” method:

console.log(a/b);

Output

Example 3: Float Dividend with Integer Divisor
In this example, we will divide the floating point value “124.72” with the integer “3” using the division operator:

const a = 124.72;
const b = 3;

console.log(a/b);

Output

Example 4: Float Dividend with Float Divisor
Now, the variables that contain float values “14.72” and “2.2” respectively:

const a = 14.72;
const b = 2.2;

We will divide both variables using the “/” division operator:

console.log(a/b);

The output indicates that if we divide two floating point numbers, it will give result in a floating point number:

Let’s move toward the second approach!

Method 2: Divide Two Numbers Using parseInt() Method

The “parseInt()” is a JavaScript predefined method that takes a value in a string format and returns it in integer format. For example, if you can pass a floating point number “10.87” as a value, it will return “10”. For dividing two numbers using parseInt(), the method first returns the number as an integer format and then applies division to it with the help of the division operator.

Syntax
Use the given syntax for dividing two numbers using parseInt() method:

parseInt(a)/parseInt(b);

Here, the “parseInt()” method takes values in integer or decimal format and returns it in integer format and then divides them using the division operator.

Example 1: Integer Dividend with Integer Divisor
In this example, we will divide the two numbers “a” and “b” by assigning integer values “41” and “2”:

const a = 41;
const b = 2;

Then, call the parseInt() method with division operator and stores its result in a newly created variable “res”:

const res = parseInt(a)/parseInt(b);

Here, parseInt() takes an integer value, so it returns the same values. When we divide them, it returns either an integer value or a decimal number based on the number.

Then, print the value of “res” with the help of the “console.log()” method:

console.log(res);

The output gives “20.5”, which is the decimal number because the dividend is an odd integer number and the dividend is an even integer number:

Example 2: Integer Dividend with Float Divisor
Here, we will divide the integer value with the float value, where the value of the variable “a” is “40” and “b” is “2.8”:

const a = 40;
const b = 2.8;

Then, call the parseInt() method with the division operator and store its result in a newly created variable “res”. This method first converts the decimal number to an integer and then divides them:

const res = parseInt(a)/parseInt(b);

Lastly, we will print the resultant value that is stored in a variable “res”:

console.log(res);

Output 

Example 3: Float Dividend with Integer Divisor
In this example, our divisor comprises floating point number and dividend is an integer:

const a = 40.567;
const b = 2;

Here the parseInt() method will first convert the decimal number to an integer and then divides them:

const res = parseInt(a)/parseInt(b);

Finally, print the resultant value that is stored in a variable “res”:

console.log(res);

Output

Example 4: Float Dividend with Float Divisor
Now, our variables contain float values “40.567” and “2.5” respectively:

const a = 40.567;
const b = 2.5;

Call the parseInt() method with the division operator and store the resultant value in a variable “res”. The parseInt() method will first convert the decimal number to an integer and then divides them:

const res = parseInt(a)/parseInt(b);

Then, print the resultant value that is stored in a variable “res”:

console.log(res);

Output

We have compiled all the methods for dividing two numbers in JavaScript.

Conclusion

For the division of two numbers, you can use the Division (/) operator or parseInt() method. The parseInt() method returns any number in an integer format and divides them using the division (/) operator. The quotient will be an integer if the dividend and the divisor are even numbers; if one is odd and the other is even, it will return a decimal number. This tutorial illustrated the methods for dividing two numbers with detailed examples.

Share Button

Source: linuxhint.com

Leave a Reply