Small Anonymous Functions

Give a small marks rule to a collection operation

The teacher wants to add a two-mark bonus to each student result. The rule is short enough to write beside the operation that uses it, rather than defining it far away.

An anonymous function is a function expression without a declared function name. JavaScript arrow functions and Python lambda expressions can express small operations, but their syntax and behavior differ.

  1. Read a short function

    Identify its input and returned expression.

  2. Use the right syntax

    Distinguish JavaScript arrows from Python lambda.

  3. Choose readable code

    Prefer a named function when an operation needs explanation.

One input becomes one adjusted result

  1. Parameter

    mark names the current input.

    35
  2. Expression

    Calculate the bonus-adjusted value.

    mark + 2
  3. Returned result

    The receiving map operation collects it.

    37
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

Short syntax still defines a real function

In JavaScript, mark => mark + 2 defines a function with one parameter and an expression body. An expression body returns its value automatically. A body in braces needs an explicit return when a result is required.

A variable can hold an arrow function, giving the operation a useful name. Anonymous describes the expression form; it does not mean the code cannot be named or reused.

Before starting, review array callback operations. These examples use JavaScript; other languages can use different syntax and rules.

Compare an expression body with a block body

Both versions return the adjusted mark and produce the same list. The braces in the second version allow statements, so return is written explicitly.

No original mark is changed: map receives numeric results and builds a new array.

JavaScript · local teaching example

const marks = [35, 40];
console.log(marks.map(mark => mark + 2).join(","));
console.log(marks.map(mark => {
  return mark + 2;
}).join(","));

Expected output

37,42
37,42

A missing block return loses the result

This callback calculates a value but does not return it. Both entries in the mapped array become undefined. Mapping the result to String makes that absence visible in the output.

JavaScript · local teaching example

const adjusted = [35, 40].map(mark => { mark + 2; });
console.log(adjusted.map(String).join(","));

Expected output

undefined,undefined

Apply the rule to a small change

Write a concise callback that selects marks below 25 from 20, 25 and 40.

Worked answer

The comparison mark < 25 returns a boolean. filter keeps only 20; the boundary mark 25 is passing under the rule and is excluded.

JavaScript · local teaching example

const marks = [20, 25, 40];
const needsHelp = marks.filter(mark => mark < 25);
console.log(needsHelp.join(","));

Expected output

20

Python uses a different single-expression form

Python · This lambda receives mark and returns mark + 2. Python lambda contains one expression; it does not support a statement body like a JavaScript arrow with braces. Use def for a multi-statement Python function.

Expected output is 37.

add_bonus = lambda mark: mark + 2
print(add_bonus(35))

Recognize mistakes and limits

JavaScript arrows capture this from the surrounding scope and cannot be used as constructors. Ordinary functions have different this behavior. Do not replace methods mechanically merely to shorten their syntax.

Returning an object literal from an arrow expression needs parentheses, such as name => ({name}). Otherwise braces are read as a function body.

A short expression can still be difficult to read. Give a complex school policy a named function and a clear explanation instead of packing several decisions into one line.

For the language or tool rules used here, see JavaScript function behavior.

Check the core idea

Why does mark => { mark + 2; } produce undefined?
Choose one answer. You can try again.

Answer and explanation

The block body has no return statement.

Correct. Evaluating the arithmetic does not send it back to the caller.

Apply the idea to a new situation

Which Python function form suits several statements and a validation branch?
Choose one answer. You can try again.

Answer and explanation

A named def function.

Correct. Its body can contain the separate statements and branch.

Make a small operation easy to recognize

Concise functions work well for one clear expression. Check return syntax, language-specific behavior and readability before choosing the shorter form.

Connect this idea with named functions for larger tasks and passing an operation to another function.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Pure Functions and Unchanged Inputs →