JavaScript Inches and Centimeters Converter

One inch equals exactly 2.54 centimeters. Multiply inches by 2.54 to get centimeters, or divide centimeters by 2.54 to get inches.

const centimeters = inches * 2.54;
const inches = centimeters / 2.54;
Inches and centimeters conversion illustration

Inches and centimeters conversion formulas Top ↑

// Inches to centimeters
centimeters = inches * 2.54;

// Centimeters to inches
inches = centimeters / 2.54;

Reusable conversion functions Top ↑

function inchesToCm(inches) {
  return inches * 2.54;
}
function cmToInches(cm) {
  return cm / 2.54;
}

Use the input event for live conversion Top ↑

The older page combined blur and keyup. For a numeric input that should update while its value changes, the input event covers typing and other value changes more directly.

inchInput.addEventListener("input", () => {
  const inches = Number(inchInput.value);
  if (Number.isFinite(inches)) {
    cmInput.value = (inches * 2.54).toFixed(4);
  }
});

Validate the current field before conversion Top ↑

Check an empty value separately, then convert and verify that the result is finite. This avoids showing NaN to the user.

See numeric validation for the difference between global isNaN() and Number.isNaN().

Keep calculation precision separate from display precision Top ↑

The conversion factor should not be shortened. If you want fewer visible decimals, format the output after calculation rather than changing the factor.

Keep both fields clearly labeled Top ↑

Visible labels identify the units without relying only on placeholders. The converter also uses type="number" with step="any" so decimal input is supported.

Continue with feet and meters conversion, getElementById(), and toFixed().

JavaScript Math Reference




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer