Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lifetime of a Variable
When you declare a variable in a function it can be accessed only in that function. (As promised, you will learn about functions shortly.) After the function has run, you cannot call the variable again. Variables in functions are called local variables.

Because a local variable works only within a function, you can have different functions that contain variables of the same name (each is recognized by that function only).

If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared and ends when the page is closed.

Local variables take up less memory and resources than page-level variables because they require only the memory during the time that the function runs, rather than having to be created and remembered for the life of the whole page.

Forum Jump:

Users browsing this thread: 1 Guest(s)