From the course: JavaScript Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Scope

Scope - JavaScript Tutorial

From the course: JavaScript Essential Training

Start my 1-month free trial

Scope

- [Narrator] In the MDN web docs, VAR is defined as a globally scoped variable. That means if you first assign a value to have VAR, and then inside a function, assign it a new value, that new value inside the function takes effect in all corresponding code. Even outside the function. This can cause what's known as scope issues, which result in unintended behavior errors, and a lot of frustration, as we try to figure out, why things aren't working properly. Let me show you a practical example of how this works. In the exercise files, I've set up these two boxes, ah, the color and the text in here, is populated using JavaScript. Looking at the script, it's straightforward, We set up a VAR, with the name color, and then we populated with a color in this click case, purple. This is a CSS color, which is why it works. You can also put in a hex value, if you want it to here, and it'll work the same way. Then…

Contents