JavaScript(JS) JS convert celsius to fahrenheit

www.igi‮editf‬a.com

To convert a temperature from Celsius to Fahrenheit in JavaScript, you can use the following formula:

°F = (°C * 1.8) + 32

Here's an example function that takes a temperature in Celsius and returns the equivalent temperature in Fahrenheit:

function celsiusToFahrenheit(celsius) {
  let fahrenheit = (celsius * 1.8) + 32;
  return fahrenheit;
}

console.log(celsiusToFahrenheit(0)); // Output: 32
console.log(celsiusToFahrenheit(100)); // Output: 212
console.log(celsiusToFahrenheit(-40)); // Output: -40

In the example above, we define a function celsiusToFahrenheit that takes one argument celsius, which is the temperature in Celsius to convert. The function calculates the equivalent temperature in Fahrenheit using the formula above and returns the result.

We then call the celsiusToFahrenheit function with several test cases to demonstrate how it works. The function correctly converts 0°C to 32°F, 100°C to 212°F, and -40°C to -40°F.