JavaScript function

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

A function declaration creates a reusable block of code with a name. You call it later with parentheses. Declarations are hoisted, so you can call the function before its line in the same scope.

JavaScript Syntax

JavaScript
function greet(name) {
  return "Hello, " + name;
}
console.log(greet("Ada"));
Try it Editor

How It Works

  1. Declare. function greet creates a binding named greet.
  2. Parameter. name receives the argument.
  3. Return. The call expression becomes the returned string.

Examples

Example 1 — Multiple parameters

Pass more than one argument.

JavaScript
function add(a, b) { return a + b; }
console.log(add(2, 3));
Try it Editor

How It Works

  1. a and b receive 2 and 3.
  2. return sends 5 back to the caller.

Example 2 — Hoisted call

Declarations can be called earlier in the same scope.

JavaScript
console.log(double(4));
function double(n) { return n * 2; }
Try it Editor

How It Works

  1. The function binding exists for the whole scope.
  2. The call runs after the engine creates the function.

Example 3 — Vs function expression

Expressions are not hoisted the same way.

JavaScript
const f = function (n) { return n + 1; };
console.log(f(1));
Try it Editor

How It Works

  1. f holds a function value.
  2. Call f like any other function.

Frequently Asked Questions

A named function created with the function keyword at statement position.
Yes. The whole function is available throughout its scope.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful