Home >>Javascript Tutorial >JavaScript Function

# JavaScript Function

## JavaScript Function

Functions in JavaScript are used to execute operations. JavaScript functions can be called multiple times in order to reuse the code.
Let's get to know the advantages of JavaScript functions:
Mainly there are 3 advantages of JavaScript functions.

• Reusability of code: A function can be called multiple times hence, saving the code.
• Less amount of coding: There is no need of writing multiple lines of code each time to execute a common task hence, it makes the program compact.
• Debugging becomes easier: Codes are divided in to blocks hence debugging a code becomes easier.

### Syntax of JavaScript Function

The syntax for declaring a function is displayed below:

```function functionName([arg1, arg2, ...argN])
{
//code to be executed
}
```

Please note that JavaScript functions can have zero or more arguments.

#### Example of JavaScript Function

Let's take an example to understand the JavaScript Function that doesn't contain any arguments.

```
<html>
<title>JavaScript Function</title>
<script>
function msg()
{
}
</script>
<body>
<input type="button" onclick="msg()" value="Welcome"/>
</body>
</html>
```
Output :Output of the above mentioned example is a call button named ‘Welcome’ that displays the message ‘Welcome to PHPTPOINT whenever the button is clicked.

### Arguments In Javascript Functions

A function can be called in JavaScript by passing Arguments. Here is one example of a JavaScript Function containing one argument.

```<html>
<title>JavaScript Function</title>
<script>
function getcube(Integer)
{
}
</script>
<body>
<input type="button" value="get value" onclick="getcube(6)"/>
</body>
</html>
```
Output :The output of the above mentioned example is the clickable button named ‘get value’ that diplays the cube of the number 6 that equals to 216.

### JavaScript Function with Return Value

Here, we can use the value returned by the function and it can be used in the program. Here is one example of a JavaScript Function that returns a value.

```<html>
<title>JavaScript Function</title>
<body>
<script>
function getInfo()
{
return "Hello PHPTPOINT! How are Things?";
}
document.write(getInfo());
</script>
</body>
</html>
```
Output :Output of the above example is a text displayed as “Hello PHPTPOINT! How are Things?”

### Object Of Javascript Function

The main motive of Function Constructor in JavaScript is creating a new Function object that executes the code globally.

A function is created dynamically in an unsecured way, if the constructor is called directly.

Here is the Syntax

```new Function ([arg1[, arg2[, ....argn]],] functionBody)
```

Here are the Parameters
arg1, arg2, .... , argn – these represents the argument used by the function.
functionBody - It depicts the definition of the function.

### Function Methods In Javascript

Method Description
apply() In order to call a function that contains this value and a single array of arguments.
bind() In order to create a new function this method is used.
call() In order to call a function that contains this value and an argument list.
toString() This returns the result in the form of a string.

### Examples of JavaScript Function Object

1. 1.Here is an example to display the sum of given numbers.
```<script>
```<script>
{"ft_lang_label":"__label__en","ft_lang_prob":0.6356123,"math_prob":0.7000498,"size":2586,"snap":"2021-31-2021-39","text_gpt3_token_len":598,"char_repetition_ratio":0.18125483,"word_repetition_ratio":0.10644258,"special_character_ratio":0.25483373,"punctuation_ratio":0.13938053,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97959566,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-18T17:23:27Z\",\"WARC-Record-ID\":\"<urn:uuid:df38a316-2331-4a73-9fc5-42407943a88c>\",\"Content-Length\":\"73723\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ce3f6290-2039-436d-87aa-cd9a35164630>\",\"WARC-Concurrent-To\":\"<urn:uuid:91e966f8-803c-4bde-bfb7-87d592492a2f>\",\"WARC-IP-Address\":\"18.232.245.187\",\"WARC-Target-URI\":\"https://phptpoint.com/javascript-function/\",\"WARC-Payload-Digest\":\"sha1:IBY3LI6FIM4DC5QRPL4BOJQYDIBFGVQJ\",\"WARC-Block-Digest\":\"sha1:IAXLH5AQKXUIKMFCRONST5EVBFP3LM6S\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780056548.77_warc_CC-MAIN-20210918154248-20210918184248-00559.warc.gz\"}"}