ConvertersLesson 322Free preview
Convert Weight
This lesson is part of How To · All levels
Summary
The weight converter converts values between kilograms, grams, pounds, and ounces through JavaScript. The conversion formula is applied after the input value is read as a number, and the result is written to the output element. Always validate the input so that the empty or non-number value does not result in misleading output.
Key points
- Use parseFloat to convert input text into numbers
- One kilogram is equal to 2,20462 pounds.
- One pound is equal to 0.453592 kilograms.
- Handles the empty value or NaN before counting
- Display the units on the result so as not to be ambiguous
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.