Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

What's the difference between assigning express() the function and express the variable to var app?

'use strict';

var express = require('express');

var app = express();

I would have thought the 3rd line would be var app = express;
since it was declared as a variable (object) above.

Can someone clear this up?

2 Answers

Steven Parker
Steven Parker
243,656 Points

In JavaScript, functions are "first class objects".

This means that functions can be assigned to variables, passed as parameters, and also returned as values.

So if a function (like "require") returns another function when you call it, then when you assign that to a variable (like "express" in your example) that variable can be used to invoke the returned function.

So the line var app = express(); first invokes the function in express, then assigns the result of calling that function to the variable "app". Without the parentheses, "app" would become a copy of the express function.

Make sense now? I also found this neat article that goes into more depth and with several examples.

That's because you're calling the function express(). Also mentioned here.