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 trialDarko Gregurek
912 PointsNode js documentation http.get
Present documentation for of Node.js http.get and https.get is different then in this course, here is an example:
http.get('http://www.google.com/index.html', (res) => {
console.log(`Got response: ${res.statusCode}`);
// consume response body
res.resume();
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
I am pretty stuck at the moment, What are $ and => signs, how to use it? Please help.
2 Answers
jobbol
Full Stack JavaScript Techdegree Student 17,885 PointsArrow Functions
The => operator is called an Arrow Function. Think functions, but quicker to type, and doesn't change the value of this. They just added the arrow with the introduction of Javascript 6.
//arrow function
var sum = () => 1 + 2;
//same as regular function
var sum = function() {
return 1 + 2;
};
Template Strings
The ${ } syntax refers to a template string. Also new. It lets you run code from within a string. It must be inside a string with backticks(`) instead of quotes(' or ").
//string concatenation
var name = 'Brendan';
console.log('Yo,' + name + '!');
//template strings
var name = 'Brendan';
console.log(`Yo, ${name}!`);
rydavim
18,814 PointsIf you want to use the documentation syntax Darko Gregurek, this is an excellent explanation by Josh Olsen. :)
Darko Gregurek
912 PointsThank you very much for your help!
Darko Gregurek
912 PointsI got it, thank you very much, I understand syntax now.
rydavim
18,814 PointsThey use a bit of shorthand in the documentation that can be hard to follow. You can think of that code as being the same as the code below:
http.get('http://www.google.com/index.html', function(response) {
console.log('Got response: ' + response.statusCode);
// consume response body
response.resume();
}).on('error', function(error) {
console.log('Got error: ' + error.message);
});
Let me know if that's still confusing though, and we can work on whatever your current project is so you can see some practical examples. Happy coding! :)
Darko Gregurek
912 PointsThank very much for your help!
rydavim
18,814 Pointsrydavim
18,814 PointsI've added some code formatting to your post. You can do this using the following markdown:
```language
your code here
```