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 jQuery Basics (2014) Creating a Simple Drawing Application Perform: Part 5

Nikolay Komolov
Nikolay Komolov
23,033 Points

Uncaught error on moving a mouse: Cannot read property 'offsetX' of undefined

Hello! Could you help to understand, please?

If I comment the 'mouseDown' condition, when I move a mouse on the canvas, the console prints out a bunch of errors: Cannot read property 'offsetX' of undefined (where undefined should be the 'lastEvent' variable on line 59). Here is my workspace:


I would like to understand why it happens. Thanks :)

1 Answer

29,445 Points

That's because lastEvent is undefined.

var lastEvent;  

console.log(lastEvent); //prints out undefined

when you define variable like that it's value is undefined until you assign something to it. If you comment out mousedown, there is nothing that writes any value to it so it stays undefined. :)