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 Node.js Basics (2014) Building a Command Line Application Making a GET Request with http

why does request even run?

if you do this:

var fun = function(){console.log("lol")}

nothiong's gonna log since we only define fun but we don't call it. So why is this executed?

var request = http.get("http://teamtreehouse.com/" + username + ".json", function(response){ console.log(response.statusCode) })

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points
var fun = function(){console.log("lol")}

In the above code, you're assigning a function to a variable, and you're correct, it doesn't execute until you invoke it using fun()

var request = http.get("http://teamtreehouse.com/" + username + ".json", function(response){ console.log(response.statusCode) })

This line however, you're NOT assigning a function to variable request , this line is kinda long and messy, perhaps if we simplify it a bit, it'll help you see it more clearly.

var request = http.get("do something");

Which is similar to this following line.

var number = Math.abs(-3);

It's just a method call on an object then assigning its value to a variable.

"It's just a method call on an object then assigning its value to a variable" - so if its assigning its value to a variable and we dont log the variable why is the value printed in the console?

William Li
William Li
Courses Plus Student 26,868 Points

ok, that's not a good example, I updated it to use Math.abs() instead, hope it makes more sense.