Simple JavaScript project with source code | Unit Converter

Javascript is one of the top scripting languages. JavaScript has lots of libraries and frameworks. Beginners can create different types of projects easily using javascript. In this tutorial, we will create a simple javascript Unit Converter project. You can download source code freely.

Some simple javascript project for beginners

You may already know using javascript we can create different types of projects. And this project can be simple or hard. At is initial stage beginners can build some simple javascript project like

Unit conversion

In our real-life, we use different types of length unit kile meter, kilometer, centimeter, etc. Also, we need to convert thous using based on our requirements.

In this project, we will convert

  • Kilometers to Centimiters
  • Kilometers to meter
  • Meter to Kilometers
  • Meter to Centimeters
  • Centimiters to Kilometers
  • Centimeters to meter

The unit value of kilometers, Centimeters, and meters

1 kilometer 100000 Centimiters 1000 meter
100000 Centimiters 1 kilometer 1000 meter
1 meter 100 Centimiters 0.001 kilometer

From this table, we can easily convert length units mathematically using pen and paper.

unit converter using javascript

On the image, we see two events

  1. When we type on this input box result shown in the result box.
  2. When we change the option from the select box.

So our idea is when type on the input box or change options we will take the value from the input box, and the value of select boxes (from, to). Then based on option value and conversion equation(from the table) we can easily solve this problem. Finally, we will set the value to our result box.


The input value is 5
optoin_from = "meter"
option_to = "centimiters"
So the answer will be
answer_value_in_centimiters = 100 x 5 = 500 centimiters

if(option_from === "meter" && option_to==="centimeter"){
	//this is meter to Centimeter formula 
	result.value = Number(input.value) * 100; // set answer to the result box

Similarly if

optoin_from = "kilometer"
option_to = "meter"

if(option_from === "kilometer" && option_to==="meter"){
	//this is kilometer to meter formula 
	result.value = Number(input.value) * 1000; // set answer to the result box
Still you face problems, feel free to contact with me, I will try my best to help you.