Functions Used as Variables

In JavaScript, you can use functions the same way as you use variables. You can use:
   var text = "The temperature is " + toCelsius(77) + " Celsius";

Instead of:
   var x = toCelsius(32);
   var text = "The temperature is " + x + " Celsius";

HTML file: Displayed by browser:
<body>
<p id="demo"></p>
<script>
   document.getElementById("demo").innerHTML =
   "The temperature is " + toCelsius(77) + " Celsius";
   function toCelsius(fahrenheit) {
    return (5/9) * (fahrenheit-32);
   }
</script>
</body>

More about functions later in this tutorial.

BackTable of ContentsNext