Functions in JavaScript.

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:

  1. The keyword “function” signifies the start of the function declaration. Additionally, it is a mandatory part of any function declaration.
  2. The function name is specified next to the keyword function. We can omit this part for certain function types.
  3. The parameters of the function come within the parenthesis. Moreover, they are an optional part of the function declaration.
  4. 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.
  5. 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.

Related Posts

What are custom events in JavaScript?

Custom events are the events that allow you to decouple the code you want to run after a specific piece of code runs. There are various in-built events…

How to use nested for loop in JavaScript?

We use the for loop statement of JavaScript for repeating a set of statements inside the loop body a specified number of times. A nested for loop, as the…

What are the basic rules for JavaScript parameters?

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as…

How to stop refreshing the page on submit in JavaScript?

Using event.preventDefault() to stop page refresh on form submit In this section, we will see how to use event.preventDefault() to stop page refresh on form submission. The event.preventDefault() restricts the default…

Target a Window Using JavaScript or HTML

TARGET attribute of HTML A link’s opening named frame or window is specified using the Target attribute of the <a> anchor tag. The concluding </a> tag in…

What is the role of deferred scripts in JavaScript?

Since JavaScript is a loosely typed language, you are not required to correctly predict the kind of data that will be kept in a variable. Depending on the information…