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
Erik Nuber
20,629 PointsIs there a way to test "this" in JS.
I have been trying to figure out how to test "this" when you use it within your JS code. Even if I assign it to a variable, the console says the variable doesn't exist.
When I am stuck, I generally use the console to see what my variables are holding and, when necessary using the console to make sure I'm traversing possible.
However, I can't do that with "this".
2 Answers
Steven Parker
243,656 PointsIt says your variable doesn't exist? Or does it say it has the value of undefined — not the same thing!
Remember that this only exists in certain contexts, like event handlers and object methods.
If you have pro membership or are in the trial period, you might enjoy the Understanding "this" in JavaScript workshop.
Erik Nuber
20,629 Pointssays it's undefined. Will watch the video. I know I've seen it in the past but, it's been a while.