Convert Kilometers to Miles using JavaScript program

featured Image

In this short post, we will write a function to convert kilometers (km) to miles (mi) using JavaScript.

We know that 1 kilometer = 0.621371 miles, so we can use a simple formula of multiplying the kilometer value by 0.621371 to get miles.

The distance in miles is equal to the distance in kilometers multipled by 0.6211371

miles = kilometers * 0.621371 

Here, 0.621371 is the conversion factor.

JavaScript Program to convert Kilometers to Miles

const kilometers = 25;

// formula convert kilometers to miles 
const miles = kilometers * 0.621371

console.log(`${kilometers} kilometers = ${miles} miles.`);

Output:

25 kilometers = 15.534275000000001 miles.

Here, we have checked how many miles is 25km using the formula kilometers * 0.621371. And the result is 15.534275000000001 miles.

If you want to convert miles to kilometers, then we use the formula,

kilometers = miles / 0.621371

Conclusion:

To convert kilometers (km) to miles (mi), we have to multiply the kilometer value by 0.621371.


Related Topics:

Convert seconds to minutes and seconds (mm:ss) using JavaScript

JavaScript – Convert Days to Seconds

Convert a Unix timestamp to time in JavaScript

Convert date to long date format using Javascript

Related Posts

featured Image

Get the 10 characters from a string using JavaScript.

Here, in this article we will learn how to get the first 10 character from any string using JavaScript. Here we will be using JavaScript’s String method substring. What is…

Read more
featured Image

Convert date to long date format using JavaScript

In this article we will look into how to convert a date to a long date format using JavaScript. To convert it to a long format we will be using…

Read more
featured Image

Prevent body from scrolling when a modal is opened

Here, in this article, we will learn how to prevent the body from scrolling when a modal or a pop-up is opened using JavaScript. The scroll event is not cancelable….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *