Var laundryRoom | https://www.codecademy.com/courses/learn-javascript/lessons/scope/exercises/functional-scope?action=lesson_resume


#1

image

I don’t understand why the luandryRoom inside my function doesn’t need a var before it. like “var laundryRoom” rather than just “lundryRoom = ‘In-unit’”



Replace this line with your code. 



#2

The keyword var is used to define and create that variable. We call that declaring a variable. You can check it out here if you’d like on MDN web docs.

When you code first loads up, the variable laundryRoom doesn’t exist in memory. Right? You have declared it yet. That means you haven’t told JavaScript the name of this variable. You use var to let JavaScript know:

“Hey, I’m creating a new variable that is named laundryRoom and I’m setting it’s value to ‘Basement’ (which we call “initializing it”).”

Now JavaScript has that variable in memory. You can use it.

Within your function myApartment(), you can use that variable. How? Just call it by its name, as you did in the console.log().

Why don’t you need to use var again? Because, you already told JavaScript about it. It’s in memory now. It’s also in global scope, meaning you can use it within a function, as you have. You can reassign new values to it, as you did too on line 5.

Why did you have to put this line of code on line 4 var mailBoxNumber = 'Box 3'? Come on. Think about it.

Because it doesn’t exist yet in your program or in the function’s scope.

Does that make sense?


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.