HTML CSS JavaScript SEO Python Posts Privacy About Contact

JS Functions

A JavaScript function is a block of code designed to perform a particular task.

A function is defined once and can be called (invoked) as many times as needed. This prevents code repetition and keeps your programs organised.

Syntax:

function functionName(parameter1, parameter2) {
  // code to run
  return result;
}

Try It — Functions in Action

Preview

Key Concepts

ConceptDescriptionExample
DefinitionDeclare the functionfunction add(a, b) { ... }
ParameterInput the function receivesa and b above
ReturnValue the function sends backreturn a + b;
InvocationCalling the functionadd(3, 5)8

Tip: Functions that return a value can be used directly in expressions: let result = multiply(4, 3) + 10;