JS Example for Beginners: JavaScript Program to Convert Kilometers to Miles

Hits: 143

(JavaScript programming Example for Beginners)

JavaScript Program to Convert Kilometers to Miles

In this example, you will learn how to convert the kilometers value to miles in JavaScript.


We know that 1 kilometer is equal to 0.621371 miles.

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

miles = kilometers * factor

Example 1: Kilometers to Miles

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

// conversion factor
let factor = 0.621371

// calculate miles
let 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 the factor to convert into miles.


To convert miles to kilometers, you can use formula:

kilometers = miles / factor

 

 

JS Example for Beginners: JavaScript Program to Convert Kilometers to Miles

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.