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:
- Page navigation (Redirect) in JavaScript
- Dialogs - Alert, Prompt, Confirmation in JavaScript
- Void keywords in JavaScript
- Print pages in JavaScript
- Objects in JavaScript
- Tutorial for creating slideshows in JavaScript with 3 easy steps
- 7 Framework JavaScript for mobile application development
- Introduction to 2D Array - 2-dimensional array in JavaScript
- 12 extremely useful tricks for JavaScript programmers
- Arrays and objects in JavaScript are like stories and newspapers!