Here are 10 JavaScript conversion programs covering various scenarios like unit conversions, data type conversions, and currency conversions.
Each program includes an explanation of how it works.
Table of Contents
1. String to Number Conversion
Converts a string input into a number using parseInt() and parseFloat().
let strNumber1 = "42"; let strNumber2 = "3.14"; let intNumber = parseInt(strNumber1); let floatNumber = parseFloat(strNumber2); console.log(intNumber); // Output: 42 console.log(floatNumber); // Output: 3.14
Explanation:
parseInt() converts the string “42” to an integer (42).
parseFloat() converts the string “3.14” to a floating-point number (3.14).
2. Number to String Conversion
Converts a number into a string using toString() or string templates.
let num = 123; let numToString1 = num.toString(); let numToString2 = `${num}`; // Using template literals console.log(numToString1); // Output: "123" console.log(numToString2); // Output: "123"
Explanation:
toString() converts the number 123 into the string “123”.
Using template literals (`${num}`) also converts the number to a string.
3. Temperature Conversion (Celsius to Fahrenheit)
Converts a temperature from Celsius to Fahrenheit.
function celsiusToFahrenheit(celsius) { return (celsius * 9/5) + 32; } let celsius = 30; let fahrenheit = celsiusToFahrenheit(celsius); console.log(`${celsius}°C is ${fahrenheit}°F`); // Output: "30°C is 86°F"
Explanation:
The formula for converting Celsius to Fahrenheit is F = (C * 9/5) + 32.
4. Temperature Conversion (Fahrenheit to Celsius)
Converts a temperature from Fahrenheit to Celsius.
function fahrenheitToCelsius(fahrenheit) { return (fahrenheit - 32) * 5/9; } let fahrenheit = 86; let celsius = fahrenheitToCelsius(fahrenheit); console.log(`${fahrenheit}°F is ${celsius.toFixed(2)}°C`); // Output: "86°F is 30.00°C"
Explanation:
The formula for converting Fahrenheit to Celsius is C = (F – 32) * 5/9.
toFixed(2) rounds the output to 2 decimal places.
5. Meters to Kilometers Conversion
Converts a length in meters to kilometers.
function metersToKilometers(meters) { return meters / 1000; } let meters = 5000; let kilometers = metersToKilometers(meters); console.log(`${meters} meters is ${kilometers} kilometers`); // Output: "5000 meters is 5 kilometers"
Explanation:
1 kilometer is equal to 1000 meters, so divide the meter value by 1000.
6. Kilometers to Miles Conversion
Converts a distance from kilometers to miles.
function kilometersToMiles(kilometers) { return kilometers * 0.621371; } let kilometers = 10; let miles = kilometersToMiles(kilometers); console.log(`${kilometers} kilometers is ${miles.toFixed(2)} miles`); // Output: "10 kilometers is 6.21 miles"
Explanation:
1 kilometer is approximately 0.621371 miles, so multiply the kilometers by this factor.
7. Pounds to Kilograms Conversion
Converts weight from pounds to kilograms.
function poundsToKilograms(pounds) { return pounds * 0.453592; } let pounds = 150; let kilograms = poundsToKilograms(pounds); console.log(`${pounds} pounds is ${kilograms.toFixed(2)} kilograms`); // Output: "150 pounds is 68.04 kilograms"
Explanation:
1 pound is equal to approximately 0.453592 kilograms.
8. Currency Conversion (USD to EUR)
Converts currency from USD to EUR based on a given exchange rate.
function usdToEur(usd, exchangeRate) { return usd * exchangeRate; } let usd = 100; let exchangeRate = 0.85; // Example exchange rate let eur = usdToEur(usd, exchangeRate); console.log(`${usd} USD is ${eur.toFixed(2)} EUR`); // Output: "100 USD is 85.00 EUR"
Explanation:
Multiply the USD amount by the exchange rate to convert it to EUR.
9. Binary to Decimal Conversion
Converts a binary number (as a string) into a decimal number.
function binaryToDecimal(binary) { return parseInt(binary, 2); } let binary = "1010"; let decimal = binaryToDecimal(binary); console.log(`Binary ${binary} is ${decimal} in decimal`); // Output: "Binary 1010 is 10 in decimal"
Explanation:
parseInt(binary, 2) converts the binary string to a decimal number. The 2 indicates that the input is in base 2.
10. Hexadecimal to Decimal Conversion
Converts a hexadecimal number (as a string) into a decimal number.
function hexToDecimal(hex) { return parseInt(hex, 16); } let hex = "1A3"; let decimal = hexToDecimal(hex); console.log(`Hexadecimal ${hex} is ${decimal} in decimal`); // Output: "Hexadecimal 1A3 is 419 in decimal"
Explanation:
parseInt(hex, 16) converts the hexadecimal string to a decimal number. The 16 indicates that the input is in base 16.
These examples cover various types of conversions, including data type conversions, unit conversions (length, temperature, weight), and currency conversions.
Feel free to customize these programs for your specific use cases!