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

When will I need this?

Hello! I've just watched the video, "What is an object?" where he explains the good things about the window and how it operates with the JavaScript Console. Which seems cool and all, but when will I need this? I might be really blind or something, but I can't create a senario when I will be using these "window.document.body" commands.

I would really appriciate if someone gave me some exampels when to use the console for these things. Hopefully I don't sound THAT dumb (hehe)..

Cheers! :-)

I think the purpose of knowing the window.document.body commands is more of an example of how the DOM works which you really need to understand. Given the number of JS libraries out there I don't think many use it unless you are trying to write your own library. If you look at any of the documentation out there you will see it being used for all types of things. The best examples will probably be when you get to the videos of what "this" is.

2 Answers

In a use-case scenario, if you ever end up writing front-end code with JavaScript, (especially with jQuery), you can always use these methods to test that you're grabbing the correct DOM elements to interact with. Having the ability to also look up the properties from the console is something you'll find to be a great ability in itself for the same reason, especially when starting out.

Of course the Developer Tools in various browsers already offer the document.body output; for instance in Chrome's Elements section of the Developer Tools. Needless to say it's something very important to have, especially if you are faced with some monkey-code that's near impossible to read in the source, or even HTML that has been programatically created without a HTML source.

Overall, if you are ever in a spot where you need to grab a specific element of the page, you know how to verify that you have the right one on any site. ;)

Thanks for your reply Dennis Brown!

You're welcome. :)

Speaking from experience as a web developer, you'll probably never need it in production code. But debugging is at least 70% of the job. And being able to granularly access parts of the DOM is 100% necessary. Especially when you get into using multiple js libraries in the same site or you inherit a site that uses them. You'll sometimes get conflicts between them that cause unforeseen issues that aren't errors per-se, so there's nothing in the logs saying "here's where the issue is". In which case you'll end up testing from the console, or the dev tools and grabbing individual DOM elements just to make sure they are what you think they are.

Doing it with the document object ensures you aren't abstracted from what you THINK you're doing and what you're actually doing.

in short.. for debugging and generally knowing what's going on inside scripts that you may not even be able to quickly see the code for (if they're minified).