A Platform To Learn To Code

JAVASCRIPT FUNCTION



A function is a group of code statements which performs some specific task by taking some input or without any input. A function may return some output to the user or can do computation to result in some output..

The basic idea of the function is to reduce the amount of code and executing a code block whenever needed.

function in javascript

function is one of the fundamental building blocks of javascript. It is a set of statements which perform some task or result in some output.

To use a function you must first declare it. In javascript function is declared by :

  • function starts with function keyword
  • Then the name of function is written after a space. Function name follow same rule as variable name
  • then a list of parameters are given, enclose in parentheses and separated by commas (number of parameters could be any starting from 0)
  • Then javascript statements are enclosed in curly braces {...}

function name_of_function (parameter1, parameter2,...) {
    // javascript statements
    // javascript statements
}

Here is an example of a function that is invoked when a button is clicked.

Example

Run the code

Just defining a function does nothing. It must be invoked/called.


function invocation

A defined function does nothing until it is invoked. function can be invoked in following ways:

  • Directly invoking function in javascript. Example function_name(parameter1, parameter2,...)
  • Invoking function when some event occurs in the webpage. Example button click, mousemove, page load etc
  • Self invoking function. function calls itself also known as recursion.

Lets see an example for all kinds of function calls.

Direct function call

Run the code

function call by an event

Run the code

Self calling function

Run the code

function parameters

function parameters are the values that are taken when the function is called. Parameters are extra added information that is required and used in the function while execution of the code.

The actual values that are passed in the function are known as arguments while the variable that is declared in the function is known as parameter.

Data type of javascript function parameter could be any value of data type, example string, number, array etc

Lets see an example where function is using argument.

Function using arguments

Run the code

In the functions we can pass more or less arguments than the defined parameters.

  • When number of arguments are less than parameters then rest parameters will be undefined
  • When number of arguments are more than parameters then extra arguments will be ignored

Lets see an example to understand.

Function using less or more arguments

Run the code

Function Return Value

function can return some value when called. Generally some kind result is returned from the function. To use the return value function is assigned to some variable.

To return something from the function return keyword is used with the return value separated by space.

Function stop execution when reached to return statement.

Example

Run the code

Local variable

The variables that are defined inside the function have local scope. These variables can't be accessed outside the function.

But function can access the variable defined outside the function.

Example

Run the code