functions

 

functions

WHAT IS A FUNCTION ?

why to use function in javascript ?

JavaScript provides functions similar to most of the scripting and programming languages. In JavaScript, a function allows you to define a block of code, give it a name and then execute it as many times as you want. A JavaScript function can be defined using function keyword

  1. You can reuse code: Define the code once, and use it many times.
  2. You can use the same code many times with different arguments, to produce different results.

Example

 

Convert Fahrenheit to Celsius: function toCelsius(fahrenheit) { return (5/9) * (fahrenheit-32); } document.getElementById(“demo”).innerHTML = toCelsius(77);