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

Grant Campbell
Grant Campbell
2,245 Points

Use of [] with http.STATUS_CODES call.

I am doing the node.js basics but I am not familiar with these calls that use [], like the http.STATUS_CODES[] . I have a feeling that it has to do with him mentioning that the STATUS_CODES is an object call and not a function call? I wanted to look more into these types of calls but I am having a hard time narrowing down what I am looking for. Any point in the right direction would be great.

1 Answer

Hi Grant,

You're right that the brackets are needed because it is an object.

Here's the documentation for http.STATUS_CODES: https://nodejs.org/api/http.html#http_http_status_codes

It gives an example of how you would access the "404" property to retrieve the "Not Found" description.

If you go into your node repl you can type http.STATUS_CODES to see what it looks like.

Here's a snippet of it: (I've deleted some lines to keep it short)

> http.STATUS_CODES
{ '100': 'Continue',
  '101': 'Switching Protocols',
  '102': 'Processing',
  '200': 'OK',
...
  '404': 'Not Found',
...
  '507': 'Insufficient Storage',
  '508': 'Loop Detected',
  '509': 'Bandwidth Limit Exceeded',
  '510': 'Not Extended',
  '511': 'Network Authentication Required' }

The codes are the properties and the values are short descriptions of that code.

There are 2 ways that properties of an object can be accessed. Using dot notation and bracket notation.

The following shows both ways:

> var person = {"name": "Grant"}
undefined
> person.name
'Grant'
> person["name"]
'Grant'
>

Dot notation can only be used when the property is a valid identifier.

We can't use dot notation for the status codes because those properties are numbers and identifiers can't begin with numbers. This won't work http.STATUS_CODES.404.

In this case, bracket notation has to be used. This will work http.STATUS_CODES["404"]

Also, if the code was stored in a variable then bracket notation would have to be used.

> var code = 404
undefined
> http.STATUS_CODES[code] // this works
'Not Found'
> http.STATUS_CODES.code // doesn't work. This is looking for a property that's literally named "code"
undefined
>

This MDN page goes into more detail on accessing properties: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors

Grant Campbell
Grant Campbell
2,245 Points

Thanks a lot Jason, that will help going forward with more courses.

You're welcome. I don't know if you've taken the javascript courses but those will help a lot too.

There's a beginner javascript track here: https://teamtreehouse.com/tracks/beginner-javascript

Those may help fill in any gaps you might have.

Grant Campbell
Grant Campbell
2,245 Points

I did take it in school but I am currently going back through things and learning more in depth aspects and advanced techniques. We mostly looked at it for basic css uses and some browser games. Thanks for the suggestion though im sure theres new stuff for me in there