How Do You Create a Function in JavaScript?

Problem scenario
You want to create a function in JavaScript. This way you can use code over-and-over. How do you use a function in JavaScript?

Solution
Call this file contint.html

<!DOCTYPE html>
<html>
<body>

<h2>This demonstrates accepting input and using a function in JavaScript</h2>


<p id="demo"></p>

<script>

x = 5
var foo = prompt("Please enter a number");

function contintFunction(p1, p2) {
  return p1 * p2;   // The function returns the product of p1 and p2
}

last = contintFunction(x, foo)
alert(last)

</script> 

</body>
</html>

Leave a comment

Your email address will not be published. Required fields are marked *