Local and Global Variables (Video Version)
-
0:01 - 0:03Now that you've mastered
the basics of functions, -
0:03 - 0:07I want to talk about a topic
that can get a little tricky: -
0:07 - 0:11the difference between
local and global variables. -
0:11 - 0:14These terms may not
mean anything to you now. -
0:14 - 0:16So let's start with an example.
-
0:16 - 0:18I made this program here to show you
-
0:18 - 0:20how many inches I grew in my childhood.
-
0:21 - 0:22Since humans grow at different rates,
-
0:22 - 0:24I came up with this function, calcInches,
-
0:24 - 0:27where I can pass in
a startAge and an endAge, -
0:27 - 0:29and an inchesPerYear,
-
0:30 - 0:32and then it would calculate
how many inches total -
0:32 - 0:34were grown during that time.
-
0:35 - 0:37. . . and return it back
to whoever called it. -
0:37 - 0:40So you can see here from 0 to 8 years
-
0:40 - 0:44I call calcInches
and I pass 0, 8, and 2.5, -
0:44 - 0:48because I grew about 2.5 inches per year.
-
0:48 - 0:52And so it does a calculation,
and you can see it spits out 20. -
0:52 - 0:58Then from 8 to 16, I call it from,
and I pass it 8 and 16 and then 2 -
0:58 - 1:01because I didn't quite grow as much,
and you can see it spits out 16. -
1:02 - 1:05So this is neat, but now I
want to actually display -
1:05 - 1:08how many inches I grew, total,
in my whole childhood. -
1:09 - 1:14So how do I do that? Well, I
might start by looking at my code -
1:14 - 1:17and thinking, "Hmm, what values
do I have here?" -
1:17 - 1:20Do I have anything that looks
like it represents total inches? -
1:21 - 1:26Well, I do have this totalInches variable
inside my calcInches function, -
1:26 - 1:31so I could just output that,
see what it says; start there. -
1:31 - 1:36So let's say text(totalInches, 10, 200),
and put it down at the bottom. -
1:36 - 1:38All right, let's see, what have we got?
-
1:38 - 1:41Oh, o-oh, we've got the 'Oh noes!' guy.
-
1:41 - 1:43And he says there's a problem.
-
1:43 - 1:46totalInches is not defined.
-
1:46 - 1:48Well, that's weird, because we
-
1:48 - 1:52defined totalInches right here, right?
var totalInches =. -
1:52 - 1:54Well, the problem is that we declared
-
1:54 - 1:59totalInches inside a function.
On this line here. -
1:59 - 2:02And when we declare a variable
inside a function, -
2:02 - 2:04it's considered a local variable.
-
2:05 - 2:09It lives only inside
this function here (calcInches). -
2:09 - 2:13And the code outside the function,
all of this, it doesn't see -
2:13 - 2:16local variables inside functions.
-
2:16 - 2:18It only sees whatever gets returned.
-
2:18 - 2:21It only sees that value,
not that variable. -
2:21 - 2:25So when we try to use totalInches
outside the function, -
2:25 - 2:26it doesn't know what it is,
-
2:26 - 2:29and it says 'Hey, I've never
seen this variable before. -
2:29 - 2:31It's not defined. I can't display it.'
-
2:32 - 2:35So there is a way that we can make it
-
2:35 - 2:37so that the outside code
can see this variable. -
2:37 - 2:42And that's if we turn it from
a local variable into a global variable. -
2:43 - 2:47We can do that by moving the definition
outside of the function, -
2:48 - 2:50into what's called the global scope.
-
2:51 - 2:54And now, inside the function,
all we're doing -
2:54 - 2:58is changing the value of it each time,
not defining and declaring it. -
2:59 - 3:02So you can see that it says
'Total grown over life: 16' -
3:03 - 3:06So it found the variable because
we made it into a global variable. -
3:07 - 3:10But it's not actually the value
that we're looking for. -
3:10 - 3:12That's just the most recent value.
-
3:12 - 3:14And that's because every time
we call this function, -
3:14 - 3:18it's setting totalInches to whatever it's
calculating that time. Right? -
3:19 - 3:22So what we really want to do is,
we want a new variable -
3:22 - 3:25that we use only
for storing the overall total -
3:25 - 3:28that we add to every time we calculate,
you know, the total for a range, -
3:29 - 3:33So let's change this back
to being a local variable, -
3:34 - 3:38and let's make a new global variable
called lifeInches, -
3:38 - 3:40and we'll start it at zero.
-
3:41 - 3:45And then inside the function,
we'll add to this global variable -
3:45 - 3:49by saying lifeInches += totalInches.
-
3:49 - 3:52So we're going to add
however much we calculate -
3:52 - 3:53each time we call this function,
-
3:53 - 3:56we're going to add it
to the lifeInches global variable. -
3:56 - 3:58And then at the bottom,
-
3:58 - 4:00we'll display lifeInches:
text(lifeInches, 10, 200). -
4:00 - 4:03Tada! our total growth over life.
-
4:04 - 4:06Now that's not actually how tall I am.
I'm taller than that. -
4:06 - 4:08But that's because you know,
-
4:08 - 4:11we start off born with more than 0 length.
-
4:11 - 4:14So if I want total,
maybe I could start at 20. -
4:14 - 4:17And there you go, that's how tall I am.
-
4:17 - 4:20All right. So let's review, totalInches
-
4:20 - 4:22is what we call a local variable.
-
4:22 - 4:24And we know that because
we see the declaration of it -
4:24 - 4:27inside this function
and not outside the function. -
4:29 - 4:31And so that means that
this outside code here -
4:31 - 4:34doesn't know about this variable
called totalInches. -
4:35 - 4:38Now lifeInches is what we
call a global variable. -
4:38 - 4:40And we know that because
we see its declaration -
4:40 - 4:43outside of any function
in our global scope. -
4:44 - 4:45So try to keep this in mind when
-
4:45 - 4:47you're writing your functions
and your variables. -
4:47 - 4:49And think to yourself whether you want
-
4:49 - 4:51a local variable
for just your function to use, -
4:51 - 4:55or a global variable
for your whole program to use. -
4:55 - 4:57And don't worry if this is hard
to wrap your head around. -
4:57 - 5:00It's one of the trickiest
concepts in programming, -
5:00 - 5:02and in JavaScript in particular.
-
5:02 - 5:05And it's something you'll get better at
as you keep practicing.
- Title:
- Local and Global Variables (Video Version)
- Description:
-
This is just a screen grab of our interactive coding talk-through, prepared to make captioning and translation easier. It is better to watch our talk-throughs here:
https://www.khanacademy.org/cs/programming/ - Video Language:
- English
- Duration:
- 05:06
![]() |
Jennifer Fennerl edited English subtitles for Local and Global Variables (Video Version) | |
![]() |
Jennifer Fennerl edited English subtitles for Local and Global Variables (Video Version) | |
![]() |
Jennifer Fennerl edited English subtitles for Local and Global Variables (Video Version) | |
![]() |
Jenny Lam edited English subtitles for Local and Global Variables (Video Version) | |
![]() |
Josephine McGuire edited English subtitles for Local and Global Variables (Video Version) | |
![]() |
Josephine McGuire edited English subtitles for Local and Global Variables (Video Version) |