JavaScript functions
A function is a group of reusable code that can be called anywhere in your program. This makes it unnecessary to write the same code over and over again. It helps programmers write modular code. Functions allow a programmer to divide a large program into small and manageable functions.
A function is a group of reusable code that can be called anywhere in your program. This makes it unnecessary to write the same code over and over again. It helps programmers write modular code. Functions allow a programmer to divide a large program into small and manageable functions.
Like any other high-level programming language, JavaScript also supports all the features needed to write modular code using functions. You have seen functions like alert () and write () in the previous chapters. We have used these functions many times, but they have only been written in core JavaScript.
JavaScript also allows us to write our own functions. This section explains how to write your own functions in JavaScript.
Function definition
Before we use a function, we need to define it. The most common method for defining a function in JavaScript is by using the Function keyword, followed by a unique function name, a list of parameters (which may be blank), and a block of statements surrounded by the brackets.
Syntax
Simple syntax is as follows: