1

I'm writing a introductory JavaScript tutorial series, I have a question about terminology.

When explaining the Array.prototype methods, I've given an example of some code like:

const letters = ['a', 'b', 'c']; 
letters.forEach((value) => {
    console.log("the value is: " + value)
}); 

I've said what's happening here is:

  1. Firstly, we're calling an object function (a function that belongs to and object, in this case the object is the letters array).
  2. We pass as the first and only argument, a function: (value) => {//...} This is commonly known as a lambda or anonymous function.. (Unfortunately AWS Lambda functions, which is a service provided by Amazon confuses the terminology a bit 😭 )
  3. That function has a parameter value, that is is going to be the current element as the we iterate over the array.
  4. We print the value.

So the first question is, say instead my code looked like:

function printCurrentValue(value) {
    console.log("the value is: " + value)
}

const letters = ['a', 'b', 'c']; 
letters.forEach(printCurrentValue); 
  • Would we still call that an anonymous function? After all, it it is now assigned to a variable.
  • Would we still call it a lambda function?

That is - intuitively I would say an anonymous function is any unnamed function, whereas I would still want to say that a named function can be used as a lambda function, as would be the case here, because lambda function, if you'll excuse my vagueness, is a 'a function that produces one value from another or something', I'm sure there's a better definition.

Elsewhere in the tutorial, I want to talk about callbacks.

For example, I might have some code like:

fetch('/user')
.then((res) => res.json())
.then((json) => console.log(json)); 

Where (res) => res.json() and (json) => console.log(json) are callbacks and they are also anonymous functions - but I'm not sure it's correct to call them lambda functions.

That is, I would define a callback as 'a function that is called else where in your application'. By that definition, the lambda functions passed into an Array.prototype method are callback functions.

Is there a tidy way to differentiate between the three terms?

5
  • A function statement (as opposed to a function expression, where the name can be omitted) isn't just "assigned to a variable".
    – jonrsharpe
    Jul 17 at 8:24
  • A lambda is a mathematical concept. Javascript does not define a 'lambda' keyword as such. Functions can be used as objects themselves, and that makes them lambdas (I guess?). As for the difference between a function statement, a function expression, and an arrow function, I would avoid getting too philosophical about their theoretical differences; in my view the differences are mostly practical and javascript specific (function statements get hoisted, expressions do not, arrow functions treat 'this' differently, etc). Jul 17 at 11:01
  • "Unfortunately AWS Lambda functions, which is a service provided by Amazon confuses the terminology a bit" - And in the Azure world, they call their version of this an "Azure Function"! I can't stand it when companies try to hijack standard terms for their proprietary products.
    – Graham
    Jul 19 at 14:19
  • For a language tutorial, I would try to use the language-specific terminology. In JavaScript what you are using is specifically called an arrow function. The traditional non-arrow version of that in JavaScript is called a function expression, and such expressions may or may not be anonymous (i.e. a traditional function expression in JavaScript can also be named if you wish).
    – Brandin
    Jul 22 at 11:24
  • Similar statement for your explanation of calling an "object function". I can understand what you mean in context, that "forEach" is a function on an object, but that term in JavaScript is specifically known as a method. See forEach.
    – Brandin
    Jul 22 at 11:35
5

Wikipedia says "In computer programming, an anonymous function (function literal, lambda abstraction, lambda function, lambda expression or block) is a function definition that is not bound to an identifier."

This makes things relatively easy. For your purposes, "lambda function" and "anonymous function" are effectively synonymous. Therefore, everything you express via the => syntax is a lambda function/anonymous function, and everything you define with the function syntax isn't.

A callback is simply code that is passed to other code to be called at some later time. As you've seen, you can use both named and unnamed functions as callbacks.

The important thing to remember is that "callback" is a role that a function takes on in a specific context. It's entirely possible to call a function as a normal function and also use it as a callback elsewhere.

5
  • 3
    You can have an anonymous function with function, too: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – jonrsharpe
    Jul 17 at 8:19
  • So, if I go change what wikipedia says, this answer will become wrong? :p In all seriousness, I don't think this is a good distinction, and especially in javascript. A function expression is effectively a lambda. The difference between a normall function expression and a => in javascript has more to do with language semantics and how the 'this' object is treated, it is not one of lambdas vs non-lambdas. Jul 17 at 10:51
  • @TasosPapastylianou Sure, but typically, when people talk about lambdas in the context of a specific programming language, they mostly mean the syntactic facility that lets you declare inline anonymous functions in an ad hoc way, and in JavaScript, arrow functions fit that role better than the more cumbersome function expressions. Jul 17 at 13:07
  • Fair enough, we can agree to disagree (or, to agree, I guess, that the word 'lambda' to mean arrow functions specifically is misapplied). Whether or not it tends to be used a certain way stylistically, the main difference between function expressions and arrow functions in javascript is exchanging execution scope for lexical scope. Using one versus the other changes the semantics of the code. It is not simply intended to be a 'nicer looking' syntax for creating anonymous functions, except in the special case where scope is irrelevant (such as, e.g. "pure" functions). Jul 18 at 13:24
  • This is why Mozilla's JavaScript reference has no mention of lambdas. That is a much better reference for JavaScript terminology, than looking at a generic Wikipedia article. You can achieve the intent of lambdas in JavaScript ES6 using an arrow function, or if using non-ES6, by using an unnamed function expression, though arguably the => looks nicer and maybe if you squint your eyes you can pretend it is a lambda symbol.
    – Brandin
    Jul 22 at 11:45
2

Callback

A Callback function is any function passed as a parameter to another function to be executed when some condition occurs. In your example, when the Promise returned by fetch is fulfilled.

A callback may be anonymous or named, or defined using function or () => {}.

That is, I would define a callback as 'a function that is called else where in your application'. By that definition, the lambda functions passed into an Array.prototype method are callback functions.

Yup! The first parameter to Array.prototype.forEach is even named callbackFn.

Anonymous vs lambda

In software engineering in general, a lambda function and an anonymous function are the same thing. Here is the definition of anonymous function from the C2 wiki.

In a programming language, an unnamed function object (also: "function literal").

Example (in PseudoCode): "lambda(x,y){ x>y }" is an anonymous function object representing the function that tells whether its first argument is greater than its second argument.

A lambda function is understood to be the same thing because of lambda calculus, which involves anonymous functions, and because the lambda keyword is often used in specific language constructs implementing support for anonymous functions.

When we drill down a bit into Javascript specifically, there are two language constructs that implement anonymous functions.

The first one is an anonymous function expression

function() { console.log("Doing stuff") }

The second is an arrow function expression

() => console.log("Doing stuff")

While these provide language support for making anonymous functions, you can still assign names to the result.

const myFunction = function() { console.log("Doing stuff") }

In other languages, such as Java and C#, lambda function refers to a syntax similar to arrow functions. While Javascript doesn't really have a language construct with that name, arrow functions would probably spring to mind for many people because of the similarity.

In conclusion, anonymous functions and lambda functions can be said to be the same thing from a software engineering perspective, but they can also refer to specific language constructs which are not equivalent.

The code below

function printCurrentValue(value) {
    console.log("the value is: " + value)
}

Is then not an anonymous function, nor a lambda function. But if it had been

const printCurrentValue = function(value) {
    console.log("the value is: " + value)
}

Then it's still not an anonymous function, but you could say it's defined using an anonymous function expression.

As for

fetch('/user')
.then((res) => res.json())
.then((json) => console.log(json)); 

(res) => res.json() is

  • Anonymous
  • A callback
  • An arrow function

And you could say it's a lambda function, both referring to it being anonymous and referring to it being an arrow function.

3
  • It is also good to point that typeof((x) => x+2) and typeof(alert) and typeof(Object.prototype.toString) all return "Function". The difference is in the syntax used to define the function, with the exception of print having print.name returning "print". Jul 17 at 12:10
  • Yeah, even typeof(Object) === 'function', but I feel this is more about implementation details in Javascript, so maybe outside the scope of this answer?
    – Turtle
    Jul 17 at 12:30
  • 1
    "even typeof(Object) === 'function'" - that's because it's essentially a constructor Jul 17 at 13:11

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .