[AskJS] Why are lambda functions called lambda functions everywhere except in JS
design patterns - Why does JavaScript code usually use lambdas instead of named functions? - Stack Overflow
What is a Lambda in Javascript ?
javascript - lambda function vs anonymous function vs callback function - Software Engineering Stack Exchange
Why does JavaScript use so many lambda functions?
Why use the lambda function?
What does the => mean in JavaScript?
Videos
Why most js developers call them arrow functions instead of lambda functions
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.
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.