In this tutorial, we will see how to convert kilometers to miles in JavaScript.
This is pure mathematics and the conversion of kilometers to miles and vice versa can be derived from its formula.
For example, 1 kilometer is equal to the 0.621371 miles, thus we can this formula for computation in our program.
Kilometers to Miles
To convert the kilometers to the miles we will take the kilometers as input and then multiply it with the constant value.
function kmToMiles(num){ return num * 0.621371; } kmToMiles(5); // 3.106855
Miles to Kilometers
Similarly, to convert the miles to the kilometers we will take the miles as input and divide it with the constant value.
function milesToKM(num){ return num / 0.621371; } milesToKM(3.106855); // 5
You can round off the returned value as per your requirement.