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.
Read a short function
Identify its input and returned expression.
Use the right syntax
Distinguish JavaScript arrows from Python lambda.
Choose readable code
Prefer a named function when an operation needs explanation.
One input becomes one adjusted result
Parameter
mark names the current input.
35Expression
Calculate the bonus-adjusted value.
mark + 2Returned result
The receiving map operation collects it.
37
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,42A 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,undefinedApply 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
20Python 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
Apply the idea to a new situation
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.