A function is the building block for a programming language. Additionally, it comprises of a set of statements designated to accomplish a specific task. The basic idea is to put some commonly or repeatedly done tasks together and make a function. Therefore, instead of writing the same code again and again for different inputs, the user can invoke that function.
What are the Functions in JavaScript?
Unlike other programming languages, JavaScript functions are objects. In other words, it is an instance of the Function type. Consequently, it has properties and methods like other objects.
How to define functions in JavaScript?
Function in JavaScript is defined using the “function” keyword, followed by the name of the function and the optional parameters within brackets.The diagram below shows the basic anatomy of declaring a function in JavaScript:
Let’s discuss all the parts of the function declaration:
- The keyword “function” signifies the start of the function declaration. Additionally, it is a mandatory part of any function declaration.
- The function name is specified next to the keyword function. We can omit this part for certain function types.
- The parameters of the function come within the parenthesis. Moreover, they are an optional part of the function declaration.
- The function body generally comes between the curly brackets({}). Additionally, it is the part where the implementation of the main logic of the function happens.
- The last statement of the function is generally a return statement, which starts with a keyword return. The value or expression follows it, which is an expectation from the function to return.
How to Call functions in JavaScript?
Defining a function does not execute it. Describing it names the function and specifies what to do at invoking the function.
Calling the function performs the specified actions with the indicated parameters. After defining a function, the next step is to call them to make use of the function. We can call a function by using the function name separated by the value of parameters enclosed between parenthesis and a semicolon at the end. Below syntax shows how to call functions in JavaScript:
Syntax:
functionName( Value1, Value2, ..);
where,
- functionName is the name of the function which needs invoking.
- Value1, Value2 are the various parameters which the function expects.
Function Naming convention
Apart from following the above structure for a function declaration, JavaScript also enforces a few conditions about how to provide function names. Some of them are:
- Function names must start with a letter, underscore, or a dollar sign.
- After the first letter/underscore/dollar, we can use numbers.
- Function names in JavaScripts are case sensitives.
- The code of the function will come inside curly brackets.
Rules to create functions in JavaScript
In addition to the conditions for naming functions, JavaScript has also defined a few rules, which user should adhere to while defining a function. Few of those rules are:
- All the functions in JavaScript should start with a function keyword.
- Function names should be unique.
- Parameters passed to function should be comma-separated.
Advantages of Functions
Like any other language, functions also play a significant role in Javascript. Some of the benefits of using functions or methods are as follows:
- Reusability of Code: We can call the function as many times we need in the script, using this way, it saves the coding efforts.
- Ease of Maintainability: Since we are keeping reusable code in the functions and calling those functions when it is required, it will be easy to debug and maintain the code.