JavaScript Functions


JavaScript provides functions similar to most programming languages and scripts.

Before using any function, it is necessary to define it, If you want to create a function in Javascript, then the ‘keyword’ function is used.

Syntax

//defining a function
function <function-name>()
{
    // code to be executed
};

//calling a function
<function-name>();

Example

function test() {
    alert("Hello World!");
}

test();

Function Parameters

We can also pass some data through a parameter (also called function argument) to the function with which it can give us the result by performing some operation.

Syntax

//defining a function
function <function-name>(paramters_list)
{
    // code to be executed
};

//calling a function
<function-name>(paramters_list);

Example

function test(firstName,lastName) {
    alert("Hello " + firstName + " " + lastName);
}

test("Ram","Mehta");