Skip to content
Latest: Publish JavaScript Packages to NPM Like a Pro!

What Is a Function Body in Programming?

A function’s body is where you place a sequence of statements you want to execute.

function nameOfFunction() {
// function's body
}

Below are examples of how we use the function body.

How to define a function with three statements in its body

Section titled “How to define a function with three statements in its body”
function getName() {
const firstName = "Oluwatobi";
const lastName = "Sofela";
console.log(firstName + " " + lastName);
}

Try Editing It

In the snippet above, the function’s body contains three statements that the computer will execute whenever the function gets invoked.

Note: console.log() is a method we use to log (write) messages to a web console.

How to define a function with two statements in its body

Section titled “How to define a function with two statements in its body”
const bestColors = ["Coral", "Blue", "DeepPink"];
function updateMyBestColors(previousColors, newColor) {
const mybestColors = [...previousColors, newColor];
return mybestColors;
}
updateMyBestColors(bestColors, "GreenYellow");

Try Editing It

In the snippet above, the function’s body contains two statements that the computer will execute whenever the function gets invoked.