Course topics

By WebNest Studio Engineering Team

JavaScript Tutorial

JavaScript Functions

Functions are reusable blocks of code that perform a task or compute a value. JavaScript treats functions as "first-class citizens," meaning they can be assigned to variables, passed as arguments, and returned from other functions — this flexibility underlies much of the language's power, from callbacks to modern array methods.

JavaScript offers three main ways to define a function: function declarations, function expressions, and arrow functions. Each has different rules around hoisting, the this keyword, and syntax, so choosing the right form matters as much as knowing the syntax itself.

Why it matters

Functions are the unit of reuse in JavaScript, and they are also values: they are passed to event listeners, array methods and promises all the time. The three ways of writing them behave differently in small but important ways, and arrow functions in particular are in almost every line of modern code.

Function Declarations

A function declaration uses the function keyword followed by a name: function add(a, b) { return a + b; }. Function declarations are fully hoisted, meaning they can be called earlier in the code than where they are written, because the entire function definition (not just the name) is made available at the top of its scope.

Function Expressions

A function expression assigns a function to a variable: const add = function(a, b) { return a + b; };. Unlike declarations, function expressions are not hoisted with their definition — only the variable declaration is hoisted (following var/let/const rules), so calling the function before this line fails.

Arrow Functions

Arrow functions use a shorter syntax: const add = (a, b) => a + b;. Beyond brevity, arrow functions do not bind their own this — they inherit this from the surrounding (lexical) scope, which makes them especially useful inside callbacks and array methods where a regular function's this would otherwise be reassigned unexpectedly. Arrow functions also cannot be used as constructors and have no arguments object of their own.

Default and Rest Parameters

Default parameters let you specify a fallback value used when an argument is omitted or explicitly undefined: function greet(name = "friend") { ... }. Rest parameters collect any remaining arguments into a real array using ...: function sum(...numbers) { ... } lets sum(1, 2, 3) receive numbers as [1, 2, 3], replacing the older, array-like arguments object with an actual array that supports methods like map and reduce.

Examples

Declaration, expression, and arrow function forms compared, including hoisting

JavaScript
console.log(declared(2, 3)); // works: declarations are hoisted

function declared(a, b) {
  return a + b;
}

const expressed = function (a, b) {
  return a * b;
};

const arrowed = (a, b) => a - b;

console.log(expressed(4, 5));
console.log(arrowed(10, 4));
Output
5
20
6

Default parameters and rest parameters working together

JavaScript
function buildOrder(item = "widget", ...extras) {
  return `Order: ${item} with extras [${extras.join(", ")}]`;
}

console.log(buildOrder());
console.log(buildOrder("gadget", "warranty", "gift wrap"));

function sumAll(...numbers) {
  return numbers.reduce((total, n) => total + n, 0);
}

console.log(sumAll(1, 2, 3, 4));
Output
Order: widget with extras []
Order: gadget with extras [warranty, gift wrap]
10

Common Mistakes

  • Calling a function expression before its declaration line, expecting it to be hoisted the same way a function declaration is.
  • Using an arrow function as an object method that needs its own "this", then being confused when "this" refers to the outer scope instead of the object.
  • Placing a rest parameter anywhere except last in the parameter list, which is a syntax error — only one rest parameter is allowed, and it must come last.
  • Confusing default parameters with required validation — a default only applies when the argument is omitted or explicitly undefined, not for other falsy values like 0 or "".

Exercise

Write an arrow function greet whose name parameter defaults to friend, and a function sum that accepts any number of arguments and returns their total.

Expected output: Hello, friend!, Hello, Asha!, 6 (one per line)

JavaScript · starter code
// TODO: arrow function greet with a default parameter

// TODO: function sum using a rest parameter

console.log(greet());
console.log(greet("Asha"));
console.log(sum(1, 2, 3));
Show hints
  • A default value is written in the parameter list: (name = "friend") => ....
  • A rest parameter, ...numbers, collects the arguments into an array, which reduce can add up.
Show solution
JavaScript · solution
const greet = (name = "friend") => "Hello, " + name + "!";

function sum(...numbers) {
  return numbers.reduce((total, n) => total + n, 0);
}

console.log(greet());
console.log(greet("Asha"));
console.log(sum(1, 2, 3));

Quick quiz

1. Which kind of function can be called before the line where it is written?

  • A. An arrow function
  • B. A function expression
  • C. A function declaration
  • D. None of them
Show answer

C. Function declarations are hoisted together with their body.

2. What does a function return when it has no return statement?

  • A. null
  • B. An empty string
  • C. 0
  • D. undefined
Show answer

D. Every call produces a value; without return it is undefined.

3. How does an arrow function treat this?

  • A. It has no this of its own and uses the one from the surrounding code
  • B. It creates its own this
  • C. this is always undefined
  • D. It sets this to the global object
Show answer

A. This is why arrow functions suit callbacks inside methods, and do not suit object methods themselves.

Interview questions

What are the differences between an arrow function and a regular function?

Show answer

An arrow function has no this of its own; it uses the this of the code that surrounds it. It also has no arguments object, cannot be used as a constructor with new, and is not hoisted, since it is assigned to a variable. A regular function gets its this from how it is called. Arrow functions are shorter and suit callbacks; regular functions are used for methods and constructors.

What is the difference between a function declaration and a function expression?

Show answer

A declaration, function name() {}, is hoisted in full, so it can be called anywhere in its scope, even above where it is written. An expression assigns a function to a variable, const name = function () {}; only the variable follows the hoisting rules of let, const or var, so the function cannot be called before that line runs.

Key Points to Remember

  • Function declarations are fully hoisted; function expressions and arrow functions are not.
  • Arrow functions inherit "this" lexically from their surrounding scope and cannot be used as constructors.
  • Default parameters supply fallback values; rest parameters gather remaining arguments into a real array.
  • Functions are first-class values in JavaScript: they can be stored in variables, passed around, and returned from other functions.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering