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 trialHara Gopal K
Courses Plus Student 10,027 Pointsproperties and methods in javascript
i was wondering if there are dir() and type() methods [like in python] in JavaScript to see all the available properties and methods or type of a JavaScript object/array
1 Answer
jeffrey bachynski
5,179 PointsYou can try typing in console.dir([1,2,3]) and open what shows up. (You can even just copy that and paste it into the console.) It is a similar result as console.log([1,2,3]).
In Chrome it should show up something like Array(3) with an arrow. If you press that arrow, you should see the contents of the array and at the bottom proto:Array(0). Pressing the arrow by that, you will get a list of prototype methods available in the prototype chain.
There is a typeof method as well, eg. typeof ([1,2,3]) but that returns object and typeof(Array) returns function in these cases.
Maybe this is what you are looking for, try running
Object.getOwnPropertyNames(Array)
that should show the property names/methods that belong directly to arrays.
Or
Object.getOwnPropertyNames(Array.prototype)
to get the prototype chain as well.
Hopefully that helps. Let me know if that is what you were looking for or if you had any issues executing the code.