What Is a Function Body in Programming?
A function’s body is where you place a sequence of statements you want to execute.
Syntax of a JavaScript Function Body
function nameOfFunction() { // function's body}
Function Body Examples
Below are examples of how we use the function body.
How to define a function with three statements in its body
function getName() { const firstName = "Oluwatobi"; const lastName = "Sofela"; console.log(firstName + " " + lastName);}
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
const bestColors = ["Coral", "Blue", "DeepPink"];
function updateMyBestColors(previousColors, newColor) { const mybestColors = [...previousColors, newColor]; return mybestColors;}
updateMyBestColors(bestColors, "GreenYellow");
In the snippet above, the function’s body contains two statements that the computer will execute whenever the function gets invoked.