Select Page

Unit conversion with javascript

Published: July 11, 2022

This tutorial will demonstrate how to convert units of measurement with the javascript method. The below examples are specific to converting a group of measurements from one unit to another with javascript. For example, if you need to convert a group of distances that were measured in miles to kilometers or a product weighed in ounces to pounds or maybe you need to convert a group of percentages to fractions. An array of these measurements can conveniently and quickly get converted with javascript’s method.

The map() method is called as a prototype method of an array. The function passed to runs on every element in that array, and then returns a new array with the returned values.

The following examples convert distance, temperature, weight, and time to demonstrate some scenarios for when and how to use the method.

  1. Convert Meters to Centimeters
  2. Convert Celsius to Fahrenheit
  3. Convert Kilograms to Grams
  4. Convert Years to Days

Let’s say we have an array of measurements in meters, but what we really need is an array of those measurements in centimeters. To convert from Meters to Centimeters multiply total meters by 100