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

Explain how to read general Syntax in the developer.mozilla.org docs

I am trying to get used to reading the developer.mozilla.org documentation and I hope someone can explain the following: DOCUMENTATION: " var newArray = arr.filter(callback(element[, index[, array]])[, thisArg]) "

I understand this filter method can take a function and it will return a new array. Does the callback take an element, possibly an array or the index of an array? How should I interpret the last part where it says : [, thisArg]?

My goal is to learn how to interpret the documentation.

1 Answer

Steven Parker
Steven Parker
230,248 Points

Right below the "Syntax" line, there's a section titled "Parameters" which contains an explanation for each term. This last one is:

thisArg Optional
Optional. Value to use as this when executing callback.

I realize there is a section there. However I was looking for a better explanation. For example, what does this mean : [, index] I appreciate any help..

Steven Parker
Steven Parker
230,248 Points

The short answer is that a parameter shown in brackets is an optional parameter.

But it sounds like you might want the documentation for the documentation itself. See this page that covers the structure of the Syntax Sections.

So in general, when I see brackets, they are optional. The callback is a function and the element in this case is the array which can optionally have an index, correct? Could you please explain the this Arg section?

Steven Parker
Steven Parker
230,248 Points

Not quite. The "callback" is a function, and it (optionally) accepts 3 arguments. The first is the element (just one item from the array), the second is the index (which item it is), and the third is the whole array. The bracket nesting indicates that some optional arguments can only be used if the others are present.

The (also optional) "thisArg" was explained in my original answer.

Take a look at the examples further down on the page, they can possibly be more valuable than the descriptions.

I found this online: Underlined words are considered literals, and are typed just as they appear. Square brackets ( [] ) around an argument indicate that the argument is optional. Ellipses ... are used to show that the previous argument-prototype may be repeated. An argument beginning with a minus sign - is often taken to mean some sort of flag argument even if it appears in a position where a file name could appear.

However, do you have an example about the thisArg argument and how/when to use it?