0

Are there any differences between first class functions and callback functions in javascript? I thought that first class functions were functions that were treated as regular variables and can be passed as arguments. Aren't callback functions the same?

  • 1
    A callback is just a use case of a function passed into another function – charlietfl Jul 04 '20 at 18:58
  • 3
    There is not distinction between "first-class functions" and "other-class functions". The term *first-class* describes how (all) functions work in general in the JavaScript language. – Bergi Jul 04 '20 at 18:59

2 Answers2

0

You are correct, all functions are first class in javascript and can be passed as objects. That's why callbacks are even possible. If you couldn't pass a function as an object there would be no way for another function (in a different scope) to use your passed function.

An example of using a first class function directly and as a callback.

function callsback(callbackFunc) {
    callbackFunc()
}

function firstClass() {
    console.log("I was called");
}

firstClass(); // "I was called"
callsback(firstClass); // "I was called"

nlta
  • 1,716
  • 1
  • 7
  • 17
0

A language is said to have first class function if they are treated as variables. So it means that we can return a function, we can pass a function as an argument and we can assign a function as a value to a variable.

Callback function is also a first class function going by this definition because it is also passed as an argument. But Callback functions are used to specifically handle asynchronous tasks in a synchronous single threaded language like JavaScript.