What is a Function? Simple Explanation with Examples

Published: (April 4, 2026 at 02:32 AM EDT)
1 min read
Source: Dev.to

Source: Dev.to

What is a Function?

A function is a block of code that performs a specific task.
Instead of writing the same code repeatedly, you can write it once inside a function and reuse it whenever needed.

Example

function square(number) {
  return number * number;
}
  • function is the keyword that defines a function.
  • square is the function name.
  • The parentheses contain parameters (in this case, number). If you need multiple parameters, separate them with commas.

Calling the Function

To execute the function, call it by its name:

let calculation = square(2); // => 4
console.log(calculation);    // 4
  • square(2) passes the argument 2 to the parameter number.
  • The function returns number * number, which is 2 * 2 = 4.
  • The returned value is stored in the variable calculation and then logged to the console.

Reference

0 views
Back to Blog

Related posts

Read more »

Understanding Functions in JavaScript

What is a Function? A function is a block of code designed to perform a specific task. Instead of writing the same code repeatedly, you can write it once insid...