This content explains a common programming error where a variable declared within an event handler (local scope) is inaccessible outside of it, leading to "Not a Number" (NaN) errors and variables not updating. The solution involves declaring variables with global scope at the beginning of the program.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
look at this little program it looks
very similar to ones we've already seen
it creates a variable and then assigns
it a value over here then there's a
non-event that allows the variable to
increase by one every time the button is
clicked finally we set the property of
clicks and that just lets us see how
it's changing over time and we've also
got this watcher down here so we can see
how the value of clicks is changing but
when we run it something weird happens
over here we're getting na n that stands
for not a number and down here we're
seeing that clicks isn't changing at all
now the solution of fixing this problem
is pretty easy and for now it's the only
thing you're going to need to do if you
switch over in a text mode this is even
easier you'll see that clicks is being
created here on line one but we're also
trying to recreate it on line three we
go back in the block mode here's the
program how we're used to seeing it and
it's not that different but suddenly a
program runs as we want and we can see
it both here and down here in the watch
panel the reason that this is happening
is because of a concept called scope and
without getting too into it the basic
idea is if you create a variable inside
of a block like on event and in
particular anything that has this
function command which you're going to
learn more about later in this year it
creates a variable that has local scope
local scope means that you can only use
it in the space that it was created and
that means anywhere inside of this on
event most variables we want are going
to have global scope it's best practice
to create variables for now at the
beginning of your program like we did
here on line one while eventually you're
going to want to create some
local variables for now we don't and so
you're going to need to debug programs
the way we just did make sure you're not
using a bar inside of a non event use
your watch panel to see if strange
things are happening like a variable
isn't changing when it looks like it
should and check if you're getting weird
output for example like we got non it
not a number later in the course you'll
learn more how to use this concept but
for now happy debugging and go catch
some of those local variables to remove
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.