Functions in Javascript
Functions in Javascript
Functions in JavaScript
October 27, 2018 Tanmay Sakpal 0 Comments functions in javascript, javascript functions, javascript
fundamentals
Functions are one of the fundamental building blocks in JavaScript.
112
Program Example 2 –
1 <html>
2 <head>
3 <title>Functions in JS</title>
4 <script type="text/javascript">
5 /*Q1) write a function to add 2 numbers and print the result */
6 function addNumber(a , b)
7{
8 var total = a+b;
9 return total;
10}
11var output = addNumber(4,2); // function call
12document.write("<h1>The total is : "+output+"</h1>");
13</script>
14</head>
15<body>
16</body>
17</html>
18
Output –
1The total is : 6
Why Functions?
You can reuse code: Define the code once, and use it many times.
You can use the same code many times with different arguments, to produce
different results.