One of JavaScript’s most versatile features is its inclusion of an accessible function syntax, allowing developers to define a function and then call that function within a script. Functions are useful for defining scopes, structuring logic, and encapsulating functionality for reuse, which is useful when building large scale web applications.

Examples of function

Function Declaration

It consists of the `function` keyword, followed by:

• Name of the function.

• A list of parameters to the function, is enclosed in parentheses.

• The JavaScript statements that define the function, enclosed in curly brackets, {…}.

// Function Syntax

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

Function Return

When JavaScript reaches a `return` statement, the function will stop executing.

Functions often compute a return value. The return value is “returned” back to the “caller”:

function name(parameter1, parameter2, parameter3) {
	return parameter1 * parameter2	// return statement
}

Invoking a function

The code inside the function will execute when “something” invokes (calls) the function:

It invokes when we call the declared function in our script.

function name(parameter1, parameter2, parameter3) {
	return parameter1 * parameter2	// return statement
}

name(2,2) 	// calling a function

Example 1

function name(a, b) {
	return a*b
}

name(5, 5);

// Result → 25

Example 2

function name(a, b, c) {
	return a*b*c
}

name(5, 2, 3);

// Result → 30

Fabio

Full Stack Developer / Entrepreneur

About the Author

I’m passionate about web development and design in all its forms, helping small businesses build and improve their online presence. I spend a lot of time learning new techniques and actively helping other people learn web development through a variety of help groups and writing tutorials for my blog about advancements in web design and development.

View Articles