Convert Miles to Kilometers in JavaScript

Beginner

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to convert miles to kilometers using JavaScript. The purpose of this lab is to help you understand the conversion formula and apply it to a real-world scenario. By the end of this lab, you will be able to write a function that converts miles to kilometers, which can be useful in a variety of applications.

Conversion of Miles to Kilometers

To convert miles to kilometers, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use the conversion formula km = mi * 1.609344.
  3. Implement the formula using the JavaScript code below:
const milesToKm = (miles) => miles * 1.609344;
  1. Test the function by calling it with the desired number of miles, like this:
milesToKm(5); // ~8.04672

This will return the equivalent distance in kilometers.

Summary

Congratulations! You have completed the Miles to Km lab. You can practice more labs in LabEx to improve your skills.