JavaScript Functions

JavaScript functions are used to perform operations. We can call JavaScript function many times to reuse the code.

Advantage of JavaScript function

Functions are useful in organizing the different parts of a script into the several tasks that must be completed. There are mainly two advantages of JavaScript functions.

  1. Code reusability: We can call a function several times in a script to perform their tasks so it saves coding.
  2. Less coding: It makes our program compact. We don't need to write many lines of code each time to perform a common task.

Rules for naming functions:

  • It must be case sensitive.
  • It must be start with alphabetical character (A-Z) or an underscore symbol.
  • It cannot contain spaces.
  • It cannot be use as a reserve words.

How to declare a Function:

To declare a function we have to use the reserved keyword "function", followed by its name, and a set of arguments.

JavaScript Function Syntax

The syntax of declaring function is given below.

In the above syntax, function is a reserved keyword and "functionName" is a name given to the function. JavaScript Functions can have 0 or more arguments.

JavaScript Function Example

Let's see the simple example of function in JavaScript that does not has arguments.

Test it Now

Output of the above example

Explanation:

In the above given example, we have created a function "msg" which is called by using the "onclick" event. When a user will click on this button then this function will be called and an alert box will be displayed. We can reuse the msg function as many as need in our program, so it will save memory.

JavaScript Function Arguments

We can call function by passing arguments. Let's see the example of function that has one argument.

Test it Now

Output of the above example

Explanation:

In the above example, we created a function, "getcube" having single parameter this function will be called by using "onclick" event by passing a value programmatically. When a user will click on button then this function will be called and an alert box will be displayed with calculating the cube of a given number which is 4 in above example.

Function with Return Value

We can call function that returns a value and use it in our program. Let's see the example of function that returns value.

Test it Now

Output of the above example

Explanation:

In the above example, we created a function, "getinfo" which will be called automatically when document loaded. Function will return a string "Hello Javatpoint! How are you?" and "document.write" function will help to write the returned string on the document.

JavaScript Function Object

In JavaScript, the purpose of Function constructor is to create a new Function object. It executes the code globally. However, if we call the constructor directly, a function is created dynamically but in an unsecured way.

Syntax

Parameter

arg1, arg2, .... , argn - It represents the argument used by function.

functionBody - It represents the function definition.

JavaScript Function Methods

Let's see function methods with description.

MethodDescription
apply()It is used to call a function contains this value and a single array of arguments.
bind()It is used to create a new function.
call()It is used to call a function contains this value and an argument list.
toString()It returns the result in a form of a string.

JavaScript Function Object Examples

Example 1

Let's see an example to display the sum of given numbers.

Test it Now

Output:

7

Example 2

Let's see an example to display the power of provided value.

Test it Now

Output:

8





Latest Courses