Convert Kilometers to Miles using JavaScript program
Find out how to write a JavaScript function that takes a number and converts kilometers to miles i.e km to mi.
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
Related Posts
Press Shift + Enter for new Line in Textarea
Override default textarea behavior on Enter key press without Shift, prevent new lines, and take custom actions like submitting forms instead. Still allow Shift+Enter newlines.
in vs hasOwnProperty(): Differences in Inherited Properties
The article explains the differences between JavaScript's 'in' operator and 'hasOwnProperty()' method. And also learn the use cases of both in JS.
How to Fix "ReferenceError: document is not defined" in JavaScript
The "ReferenceError: document is not defined" is a common error in JavaScript that occurs when trying to access the `document` object in a non-browser environment like Node.js.
How to Fix the "Cannot Read Property of Undefined" Error in JavaScript
The "Cannot read property of undefined" error occurs when you try to access a property or method of a variable that is undefined in JavaScript.
