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.
Console
- 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
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.
- MDN: Functions, reusable blocks of code
- MDN: Function return values
- MDN: Functions (JavaScript guide)
- MDN: Arrow function expressions
- MDN: Default parameters
- MDN: Scope
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.