Menu Close

JavaScript Function

In this tutorial, you will learn about JavaScript Function along with examples. Function in JavaScript is the most important to perform a specific task.

When we want to perform a specific task in javascript then we have an option to do that using JavaScript function, which means JavaScript function is used to perform a specific task.

What is JavaScript Function?

In JavaScript, a function is a block of code that is designed to perform a specific task. A javascript function always runs when it call or invoke.

If you don’t understand above line then don’t worry in the below example you will understand.

function myFunction(a, b)
{
 return a + b;
}

JavaScript Function Syntax

A JavaScript function is created using a keyword function followed by the name and parentheses ().
The function name can contain a letter, number, Doller sign, and understand same rule apply of a javascript variable.

You can pass parameters separated by a comma inside parentheses. The code to be executed by function is always written inside curly bracket { }.

Syntax:

function function_name(parameter1, parameter2, parameter3)
{
	// code to be executed
}

Function Invoke

The function is always invoked by function name. The code is written inside the function run when something calls the function.

Function Return

If you want to your function return some value when it is called then you can use the return statement. When javascript reaches return statement, the function will stop executing.

function myFunction(a, b)
{
 return a + b;  // Function return product of a and b.
}

Why we need to use javascript function?

Sometimes we want to use the same code multiple times throughout the program, Then we have two options first is to write the same code in multiple places, and the second method is to create a single function and call the function on multiple places so this is the best practice to write JavaScript function. The function is used for code reusability.

Invoke the function

In the below example, myFunction is referred to the function object and myFunction() function refers to the function result.

Example

<script>
function myFunction(a, b)
{
 return a + b;  // Function return product of a and b.
}	

document.getElementById("myID1").innerHTML = myFunction;
document.getElementById("myID2").innerHTML = myFunction(12, 30);
</script>

Function used as variable

JavaScript function can be also use as variable name.

Example

<script>
function myFunction(a, b)
{
 return a + b;  // Function return product of a and b.
}	

var x = myFunction(12, 30);
document.getElementById("myID1").innerHTML = x;

// Instead of storing function return value into variable, use function name directly as a variable name.

document.getElementById("myID2").innerHTML = myFunction(12, 30) + 20;
</script>

Function without parameter

Function in JavaScript can also use without any parameter.

Example

<script>
function printDetails()
{
alert("Welcome to Programming Funda JavaScript Tutorial!")
}
printDetails()
</script>

Conclusion

In this article, you have learned all about JavaScript functions along with examples. if you are curious about javascript, Then you can’t ignore the function in JavaScript because the function in javascript is going to be more helpful for you.

JavaScript function is also perfect for code reusuability, that means you use same function multiple times in your javascript code.

if you like this article, please share and keep visiting for further JavaScript tutorials.

Reference:- Click Here

Previous Article
Next Article

Was this post helpful?

Related Posts