JavaScript Program to Convert Kilometers to Miles

To understand this example, you should have the knowledge of the following JavaScript programming topics:


We know that 1 kilometer is equal to 0.621371 miles.

So to convert kilometers to miles, you can use the formula:

miles = kilometers * 0.621371

Example 1: Kilometers to Miles

// taking kilometers input from the user
const kilometers = prompt("Enter value in kilometers: ")

// conversion factor
const factor = 0.621371

// calculate miles
const miles = kilometers * factor

console.log(`${kilometers} kilometers is equal to ${miles} miles.`);

Output

Enter value in kilometers: 2.2
2.2 kilometers is equal to 1.3670162000000001 miles.

Here, the kilometers variable is used to store the kilometer value from the user. Then kilometer value is multiplied with factor to convert into miles.


To convert miles to kilometers, you can use formula:

kilometers = miles / factor

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges