In this tutorial, you will learn-
How to Create a Function in JavaScript Function with Arguments JavaScript Return Value
How to Create a Function in JavaScript
Use the keyword function followed by the name of the function.
After the function name, open and close parentheses.
After parenthesis, open and close curly braces.
Within curly braces, write your lines of code.
Syntax:
function functionname()
{
lines of code to be executed
}
Try this yourself:
Function with Arguments
You can create functions with arguments as well. Arguments should be specified within parenthesis Syntax:
function functionname(arg1, arg2)
{
lines of code to be executed
}
Try this yourself:
JavaScript Return Value
You can also create JS functions that return values. Inside the function, you need to use the keyword return followed by the value to be returned. Syntax:
function functionname(arg1, arg2)
{
lines of code to be executed
return val1;
}
Try this yourself: