JavaScript Function

Beginner
9 min read
Updated: Nov 2026
3 examples
Reusable code

Definition and Usage

A function is a block of code with a name that you can run again and again. You give it values (parameters), it does some work, and it can send a result back with return.

In JavaScript every function is also an object of the built-in Function type. That means a function can be stored in a variable, passed to another function, returned from a function, and it has its own methods and properties such as name and length.

This page explains the idea. The Function Methods index links the full guides for apply(), bind(), call() and toString().

Functions are values.

You can put a function in a variable, give it to another function as a callback, or return it. The code only runs when you call it with parentheses: greet().

JavaScript Syntax

Write a function, call it, and use the four Function methods:

JavaScript
function greet(name) {                 // function declaration
  return "Hello, " + name;
}

const shout = (text) => text.toUpperCase();   // arrow function

console.log(greet("Ava"));              // "Hello, Ava"
console.log(shout("hi"));               // "HI"

console.log(greet.call(null, "Ben"));         // run now, arguments one by one
console.log(greet.apply(null, ["Cy"]));       // run now, arguments in an array
const hello = greet.bind(null, "Di");         // new function with "Di" filled in
console.log(hello());                         // "Hello, Di"
console.log(greet.toString().length > 0);     // the code as text

Core Ideas

Ideas every Function tutorial in this series builds on:

IdeaExampleRemember
Parameterfunction add(a, b) { ... }A name inside the parentheses that receives a value
Argumentadd(2, 3)The real value you pass when you call the function
returnreturn a + b;Sends a result back and stops the function
thisthis.nameThe object a function was called on; it depends on how you call it
namegreet.nameThe name of the function, as text
lengthgreet.lengthHow many parameters the function declares
Closurereturn function () { count++; }An inner function that remembers the variables around it
Callbacklist.map(double)A function given to another function to call later

Ways to Write a Function

These styles all create functions. They differ in a few small details:

StyleExampleHas its own this?
Declarationfunction add(a, b) { return a + b; }Yes
Expressionconst add = function (a, b) { return a + b; };Yes
Arrow functionconst add = (a, b) => a + b;No, it uses the surrounding this
Method shorthandconst calc = { add(a, b) { return a + b; } };Yes
Constructorconst add = new Function("a", "b", "return a + b");Yes (rarely used)

Start with declarations and arrow functions. A declaration can be called before the line where it is written, because JavaScript moves it to the top (this is called hoisting). An arrow function or an expression stored in a const cannot.

How Functions Work

Defining is not running

Writing function greet() { ... } only creates the function. Its code runs when you call it with parentheses, greet(). Without the parentheses you only get the function itself.

Arguments fill parameters in order

The first argument goes into the first parameter, and so on. A missing argument becomes undefined, and an extra argument is ignored unless you collect it with ...rest.

return ends the function

The first return that runs sends a value back and stops the function. A function without return gives back undefined.

this depends on the call

A regular function gets its this from how it is called: user.greet() uses user. An arrow function does not have its own this. The methods call(), apply() and bind() let you choose it.

Inner functions remember

A function created inside another function keeps access to the variables around it, even after the outer function has finished. This is a closure. Example 3 shows it.

Real-Life Uses

Functions are the main way to organize JavaScript code. Typical places you will use them:

UseWhat it does
Reuse codeWrite a calculation once, call it from many places with different values.
Callbacks and eventsGive a function to setTimeout(), map() or addEventListener() so it runs later.
Methods of objectsPut functions inside objects to describe what that object can do, such as cart.addItem().
Private dataUse a closure to keep a counter or setting hidden, with only a few functions allowed to change it.

JavaScript Examples

Three short programs. Each one prints plain text, so the output is the same in Node.js and in the browser console.

Example 1 — Write the Same Function Four Ways

A declaration, an expression, an arrow function and an object method all add two numbers.

JavaScript
function addDeclaration(a, b) {
  return a + b;
}

const addExpression = function (a, b) {
  return a + b;
};

const addArrow = (a, b) => a + b;

const calculator = {
  add(a, b) {
    return a + b;
  }
};

console.log("Declaration: " + addDeclaration(2, 3));
console.log("Expression: " + addExpression(2, 3));
console.log("Arrow: " + addArrow(2, 3));
console.log("Method: " + calculator.add(2, 3));
call() tutorial

How It Works

  1. Declaration. The classic form starts with the word function and a name.
  2. Expression and arrow. Both store a function in a const. The arrow form is shorter, and when the body is one expression you can leave out return and the braces.
  3. Method. Inside an object, add(a, b) { ... } is short for add: function (a, b) { ... }. You call it with the object in front: calculator.add(2, 3). All four print 5.

Example 2 — Treat a Function Like an Object

Ask a function about itself, and even give it a property of your own.

JavaScript
function greet(name, greeting) {
  return greeting + ", " + name;
}

console.log("typeof: " + typeof greet);
console.log("instanceof Function: " + (greet instanceof Function));
console.log("name: " + greet.name);
console.log("length: " + greet.length);

greet.language = "English";
console.log("language: " + greet.language);

How It Works

  1. Ask about the type. typeof greet is "function", and greet instanceof Function is true.
  2. Read built-in properties. name is the function's name, and length is the number of declared parameters, here 2.
  3. Add your own. Because a function is an object, greet.language = "English" works like it does on any object.

Example 3 — Build Counters with a Closure

makeCounter() returns a small function. Each counter remembers its own number.

JavaScript
function makeCounter(start) {
  let count = start;

  return function () {
    count++;
    return count;
  };
}

const first = makeCounter(0);
const second = makeCounter(10);

console.log(first());
console.log(first());
console.log(second());
console.log(first());
bind() tutorial

How It Works

  1. A function that returns a function. makeCounter() creates a variable count and returns an inner function.
  2. The inner function remembers. Even after makeCounter() has finished, the returned function can still read and change its own count. This is a closure.
  3. Separate memories. first and second have their own count, so first() prints 1, 2, 3 while second() prints 11.

Function Methods

Function has four methods. Each one has its own tutorial:

MethodWhat it does
apply()Run a function now with a chosen this and the arguments in an array.
bind()Create a new function with this (and the first arguments) fixed.
call()Run a function now with a chosen this and arguments one by one.
toString()Turn a function into text and read its code.

Full list: Function Methods index.

Frequently Asked Questions

A function is a reusable block of code that you can run by calling its name with parentheses. It can take values (parameters) and send a result back with return. In JavaScript a function is also an object, so you can store it in a variable and pass it around.
A declaration starts with the word function and a name, and it can be called before the line where it is written. An arrow function is shorter, is usually stored in a const, and does not have its own this. Use arrow functions for short callbacks and declarations for the main functions.
For a regular function, this is the object the function was called on: user.greet() makes this equal to user. If you call the function on its own, this is lost. call(), apply() and bind() let you choose this by hand, and arrow functions use the this of the code around them.
call() and apply() run the function immediately; call() takes arguments one by one and apply() takes them in an array. bind() runs nothing and returns a new function with this fixed, which you call later.
A closure is an inner function that remembers the variables of the function it was created in, even after that outer function has finished. It is how a counter or a private setting can be kept without using a global variable.

Browse the Function methods

Open the index for apply(), bind(), call() and toString(): syntax, runnable examples, and FAQs.

Function Methods index →

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.

16 people found this page helpful