From the course: JavaScript: Scope

What are local variables? - JavaScript Tutorial

From the course: JavaScript: Scope

Start my 1-month free trial

What are local variables?

- [Instructor] Now that we have a good understanding of global variables, what are local variables? They are variables declared inside of a function. I did mention that a couple times already, but we'll drive the point home in this video. In other words, anything you declare inside of a function is a local variable. Local variable's lifetime is from when the function is declared to when the function is completed. Therefore, when you have a variable inside of a function, when that function has completed its code, then that variable is no longer available. Also, since the local variable is deleted when the function is completed, many functions can have the same variable name. Let's explore how the syntax looks when dealing with local variables. Let's remove all this code here and let's start from scratch. Let's create a new variable called screamwarrior, which is a function, and then let's declare two variables inside of that function. The first one is called warrior1 and this one is a Ninja. Again, we're hard on the warriors here and then let's declare a second one, which is called warrior2. This one is Samurai. Then, what we'll do is return at the end of that function with template strings, our warriors are warrior1 and warrior2. And then we could log this. Let's log and run our function all at once. These functions, when you actually run this, and let's go to the browser to take a look. Everything works perfectly. So, all warriors are a Ninja and Samurai because these variables are actually available here and we're returning this with the variables inside of that function. When we run the function, these variables are actually printing in the console.log because they're available when we actually return it. If we did something like this, console.log, and try to get warrior1 or warrior2 on the console.log without leveraging the actual function, let's go back to the browser, we're gonna get an error because warrior1 is not defined. If we add warrior1 define at the top or the global scope, then it would say warrior2 not defined. Let's type a note here, so you can keep this into your notes. If you wanna go back to the exercise files, you'll see this. Warrior1 and warrior2 aren't available globally and that's why we're running into issues here. Let me come and tab that lower guy here. The second thing I wanna show you in the local variables. Because these variables are inside or locally scoped, we could use or if we could do a second function and do the exact same thing with the exact same variable. We could do screamwarrior2 and then inside of this function, use the same variables again and change those variables to something else. Let's say Viking for this guy and then for this guy let's do a Soldier, and then we could do screamwarrior2 and then screamwarrior1 or screamwarrior, the first function. Those two functions are gonna run fine because these are locally scoped, so they're available for this function, but they're not available outside. There's no impact from one to the other. If we save this, we should see the two sentences. Warriors are Viking and Soldier, warriors are Ninja and Samurai. This is a look at local variables. Let's move on.

Contents