⚡0 XP🔥0

Functions and Arrow Functions

Lesson 4 of 7 ⏱ 4 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs JavaScript

Declare and call functions, parameters, default values and return, function expressions and arrow functions, scope, callbacks and higher-order functions.

Why functions?#

A function is a named, reusable block of code. Write it once, use it many times - and give a meaningful name to a piece of logic (calculateFees, isValidEmail).

Declaring and calling#

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
Namaste, Asha!
Namaste, Ravi!
Room area: 120 sq ft
Two rooms: 200
हिंदी में समझें

Parameter function की परिभाषा में लिखा नाम है (name), और argument वह असली value है जो call करते समय भेजते हैं ("Asha")। return function से जवाब वापस भेजता है और function वहीं रुक जाता है।

Default parameters#

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
5000
4500

Function expressions and arrow functions#

Functions are values - you can store them in variables.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
16 27 7
Hi!
{ name: 'Neha', active: true }
Form Example
declaration function add(a, b) { return a + b; }
expression const add = function (a, b) { return a + b; };
arrow (one expression) const add = (a, b) => a + b;
arrow (block body) const add = (a, b) => { const s = a + b; return s; };
Tip

With a block body { } you must write return. (a, b) => { a + b } returns undefined!

Scope#

▶ Try it YourselfJavaScript
Ctrl + Enter to run

Keep variables as local as possible. Fewer globals means fewer accidental clashes.

Callbacks and higher-order functions#

A higher-order function takes a function as an argument (or returns one). The function you pass is a callback.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
Reminder 1
Reminder 2
Reminder 3
21
setTimeout doesn't block - this prints first
This prints after 0.5 seconds

makeMultiplier shows a closure: the returned function remembers k even after makeMultiplier has finished.

Recursion#

A function can call itself. It needs a base case to stop.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
120

Summary#

  • Functions bundle reusable logic; return sends a value back (otherwise undefined).
  • Default parameters: function f(x, y = 10).
  • Arrow functions: (a, b) => a + b; use { } + return for longer bodies.
  • let/const are block-scoped; closures remember outer variables.
  • Callbacks are functions passed to other functions - the heart of events, timers and array methods.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

GST calculator

Write a function withGst(amount, rate = 18) that returns the amount including GST, rounded to 2 decimals as a number (use Math.round(x * 100) / 100).

The given code prints the results - it should print 1180, 1050 and 117.94.

Expected output:
1180
1050
117.94
2

Is it a prime?

Write an arrow function isPrime that returns true if a number is prime and false otherwise. Then print the primes between 1 and 30 joined by spaces:

2 3 5 7 11 13 17 19 23 29

Expected output:
2 3 5 7 11 13 17 19 23 29

Quick Quiz

Q1.

What does a function return if it has no return statement?

Q2.

Which is a correct arrow function that doubles a number?

Q3.

What is printed? function greet(name = "Guest") { return "Hi " + name; } console.log(greet());

Q4.

A variable declared with let inside a function...

Q5.

What is a callback?