From the course: JavaScript: Scope

Unlock the full course today

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

What are global variables?

What are global variables? - JavaScript Tutorial

From the course: JavaScript: Scope

Start my 1-month free trial

What are global variables?

- [Narrator] Global variables are any functions, objects, variables that are declared outside of functions. And when we say global, we mean they are accessible throughout your file. And this is a good time to introduce the variable, lifetime. A term you may hear from time to time. The variable lifetime is when the variable is deleted after it has been declared. In the case of global variables, it is available when declared and deleted when the application is closed, in most case when the browser is closed. And for that reason, it is often not recommended to use global variables. Let's say for example you initialize a global variable with user data. This can be a privacy or security issue. As someone could literally pull data from this variable in the browser. Two nuances to global variables. If you don't declare a variable and you assign a value to a variable inside of a function, it automatically becomes globally…

Contents