Convert Miles to Kilometers in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") subgraph Lab Skills javascript/variables -.-> lab-28499{{"`Convert Miles to Kilometers in JavaScript`"}} javascript/data_types -.-> lab-28499{{"`Convert Miles to Kilometers in JavaScript`"}} javascript/arith_ops -.-> lab-28499{{"`Convert Miles to Kilometers in JavaScript`"}} javascript/comp_ops -.-> lab-28499{{"`Convert Miles to Kilometers in JavaScript`"}} end

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.

Other JavaScript Tutorials you may like