Skip to content

JavaScript Functions: Parameters, return, Default Values, Arrow Functions and Scope

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

So far every calculation in this course has been written out in full each time you needed it. Real programs cannot work like that. A VAT calculator, an end of service gratuity tool and an invoice generator all repeat the same small jobs again and again: add tax, round to two decimals, format an amount in dirhams. A function gives one of those jobs a name, so you write it once and use it everywhere. When the VAT rate or the rounding rule changes, you fix one place instead of fifty. This lesson shows how to write functions, how to pass values in and get a result out, and the rules about which variables a function can see.

Quick answer

Declare a function with function addVat(price, rate) { return price * (1 + rate); } and call it with addVat(100, 0.05), which gives 105. The names in the brackets are parameters, the values you pass are arguments, and return sends the result back to the caller and ends the function. Give a parameter a default with rate = 0.05. The short arrow form is const addVat = (price, rate = 0.05) => price * (1 + rate);. Variables declared inside a function exist only inside it, and a function can be passed to another function, as you saw with map and filter.

30 min read and practice · Content checked on 6 November 2026

In this lesson you will

  • Declare and call functions, and tell parameters from arguments
  • Return a result and use it, and give parameters default values
  • Write arrow functions and know when the short form returns by itself
  • Explain scope, and pass a function to another function as a callback

Declaring and calling a function

A function declaration starts with the keyword function, then a name, a pair of brackets and a block in curly braces: function greet() { console.log("Marhaba"); }. Writing it does nothing yet. The code inside runs only when you call the function by its name followed by brackets, greet();, and it runs again every time you call it. Name functions after what they do, usually with a verb: formatAed, addVat, isWeekend.

Parameters are the names listed in the brackets when you write the function, and they act like local variables. Arguments are the actual values you put in the brackets when you call it. In function addVat(price, rate), price and rate are parameters; in addVat(200, 0.05), 200 and 0.05 are arguments, matched in order. If you pass fewer arguments than there are parameters, the missing ones are undefined, which is why a missing argument often ends in NaN.

Try it: One function, many calls
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

The last line prints NaN because rate was not passed and is undefined. The next section fixes that with a default value.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

What is the difference between a function declaration and an arrow function?

Both make a function. A declaration, function name() {}, can be called before the line where it is written, because JavaScript sets declarations up first. An arrow function stored in a const can only be used after that line. Arrow functions are shorter and suit small callbacks; they also do not have their own this, which matters later when you work with objects.

Can a JavaScript function return more than one value?

A function returns one value, but that value can be an array or an object that holds several results, such as return { vat, total };. The next lesson, on objects, shows how to read those results back out by name.

What is a callback function?

A function you pass to another function, which then calls it at the right moment: for every item in map, when a button is clicked in addEventListener, or when data arrives from a server. You pass the function without brackets, so the other code can decide when to run it.

How many parameters should a function have?

There is no hard rule, but functions with more than three or four parameters are hard to call correctly, because the order is easy to mix up. When you need many inputs, pass one object instead, for example invoice({ price, qty, rate }), so each value is named.

Why does my function give NaN?

Usually one of the arguments is missing, so its parameter is undefined, or it is a string that is not a number, such as text from a form. Print the parameters at the top of the function with console.log to see what arrived, then add a default value or convert the input with Number().

Sources

Facts in this lesson were checked against these official pages on 6 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.