Define a Function in JavaScript



The most common way to define a function in JavaScript is by using the “function” keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.

Syntax

Here’s the syntax −

<script>
   <!--
   function functionname(parameter-list) {
      statements
   }
   //-->
</script>

Try the following example. It defines a function called sayHello that takes no parameters,

<script>
   <!--
   function sayHello() {
      alert("Hello World!");
   }
   //-->
</script>
Updated on: 2020-06-15T12:04:16+05:30

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements