Functions in JavaScript - GeeksforGeeks

22 Jun, 2020
A function is a set of statements that take inputs, do some specific computation, and produces output. Basically, a function is a set of statements that performs some tasks or does some computation and then return the result to the user.
The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can call that function.
Like other programming languages, JavaScript also supports the use of functions. You must already have seen some commonly used functions in JavaScript like alert(), this is a built-in function in JavaScript. But JavaScript allows us to create user-defined functions also.
We can create functions in JavaScript using the keyword function. The basic syntax to create a function in JavaScript is shown below.
Syntax:
function functionName(Parameter1, Parameter2, ..)
{
    // Function body
}

To create a function in JavaScript, we have to first use the keyword function, separated by name of function and parameters within parenthesis. The part of function inside the curly braces {} is the body of the function.
Function Definition
Before, using a user-defined function in JavaScript we have to create one. We can use the above syntax to create a function in JavaScript. A function definition is sometimes also termed as function declaration or function statement.
Below are the rules for creating a function in JavaScript:
Every function should begin with the keyword function followed by,
A user defined function name which should be unique,
A list of parameters enclosed within parenthesis and separated by commas,
A list of statement composing the body of the function enclosed within curly braces {}.
Example:
function calcAddition(number1, number2) {     return number1 + number2; }
In the above example, we have created a function named calcAddition, this function accepts two numbers as parameters and returns the addition of these two numbers.
Function Parameters
Till now we have heard a lot about function parameters but haven\’t discussed them in detail. Parameters are additional information passed to a function. For example, in the above example, the task of the function calcAddition is to calculate addition of two numbers. These two numbers on which we want to perform the addition operation are passed to this function as parameters. The parameters are passed to the function within parentheses after the function name and separated by commas. A function in JavaScript can have any number of parameters and also at the same time a function in JavaScript can not have a single parameter.
Calling Functions: 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:
functionName( Value1, Value2, ..);

Below is a sample program that illustrate working of functions in JavaScript:
<script type = "text/javascript">  // Function definitionfunction welcomeMsg(name) {   document.write("Hello " + name + " welcome to GeeksforGeeks");}  // creating a variablevar nameVal = "Admin";  // calling the functionwelcomeMsg(nameVal);  </script>
Output:
Hello Admin welcome to GeeksforGeeks

Return Statement: There are some situations when we want to return some values from a function after performing some operations. In such cases, we can make use of the return statement in JavaScript. This is an optional statement and most of the times the last statement in a JavaScript function. Look at our first example with the function named as calcAddition. This function is calculating two numbers and then returning the result. The most basic syntax of using the return statement is:
return value;

The return statement begins with the keyword return separated by the value which we want to return from it. We can use an expression also instead of directly returning the value.
Javascript | Arrow functions
JavaScript | escape()
JavaScript | unescape()
JavaScript | Window print()
Javascript | Window Blur() and Window Focus() Method
JavaScript | console.log()
JavaScript | parseFloat()
JavaScript | uneval()
JavaScript | parseInt()
JavaScript | match()
JavaScript | Date.parse()
JavaScipt | Replace() Method
JavaScript | Map.get( )
JavaScript | Map.entries( )
JavaScript | Map.clear( )
JavaScript | Map.delete()
JavaScript | Map.has( )
More>>
ManasChhabra2
javascript-basics
javascript-functions
JavaScript-Misc
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert a string to an integer in JavaScript
How to calculate the number of days between two dates in javascript?
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
File uploading in React.js
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Hide or show elements in HTML using display property
Difference Between PUT and PATCH Request
JavaScript | console.log() with Examples