What does it mean: "functions are first class objects in JavaScript"?


I've heard it several times: "functions are first class objects in JavaScript". Functions being stored in variables and passed in methods and etc wasn't something that surprised me so much in JavaScript initially, as I was already used to C# delegates. My question is regarding this term "first class", what exactly does it mean?


The term "first class" means that the function is treated the same as any value type in javascript. For example, for you to assign a value to a variable, you do like this:

var meuInteiro = 10;  
var minhaString = "minha string";
var minhaFuncao = function() { console.log('minha função'); }

In other words, you used the same syntax in all three forms, and javascript understands the same way. You can also pass the function as parameters, object attributes, etc, just as you do with any other type. That's what "first class" means.

— A great article on the subject: http://javascriptbrasil.com/2012/09/05/funcoes-de-primeira-classe/

Scroll to Top