tutorialstonight logo
A Platform To Learn To Code

JAVASCRIPT FUNCTION



What Is A Function In Javascript?

A function is one of the fundamental building blocks in javascript, it is a group of code statements which performs some specific task with or without taking 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 number of repeated code blocks and executing a code block whenever needed.

Javascript Function Declaration

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

  • Start function with function keyword
  • Then write the name of the function after a space. The naming convention of function is same as variable naming convention.
  • then a list of parameters are given, enclose in parentheses and separated by commas like (parameter1, parameter2, ...)
  • Then javascript statements are enclosed in curly braces {...}

function declaration syntax:

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

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

Example

Run the code

Javascript Function Expression

As we have seen above function is a statement, but we can also make function an expression.

A function expression can be anonymous, it does not have a name.

A function expression is convenient when passing a function.

Example

Run the code

Javascript function call

Just defining a function does nothing until it is invoked or called.

How to call a function in javascript?

To call a function directly in javascript write the the name of function and give required parameters. See the example below the 'sum' function defined in above example is called by giving 2 parameters:

Direct function call

Run the code

call javascript function from html

You can call a javascript function from HTML when some event occurs like button click, mouse move, document load, etc.

In the following example we have a function that alerts a message, it is invoked as page loads and when button is clicked.

Note: We can call a function from any event we want and as many times.

Example

Run the code

Javascript function parameter

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 valid data type, example string, number, array, etc

Lets see an example where function is using argument.

Example

Run the code

In Javascript functions you 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.

Example

Run the code

Javascript Function Return

function can return some value when called. To use the return value function is assigned to some variable and when function return some value it get stored in the 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

The return value can be placed anywhere in the function but as soon as function find return statement it stops further execution of the code in function.

Also, you can use return statement without returning any value, you can do this to stop function to execute further in certain condition.

Example

Run the code

Javascript Function Local Variable

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

But a function can access the variable that are defined outside the function.

Example

Run the code
points to remember Points to remember:
  1. Function in javascript is declared using function keyword.
  2. A function can have any number of arguments starting from 0.
  3. You can call javascript function from HTML using some event.
  4. As soon as function reaches return statement it stops further execution of it.
  5. Variable defined in a function has local scope.