I have a question about static variables in JavaScript. Do they only last during the scope of the script? What I mean is what if control leaves the script and goes back to the html code, are the static variables still there?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I assume that you mean global instead of static variables. Global variables are declared in the global document scope. They are accessible from all methods (functions) and when you modify their value from within a function’s code block, the value persists, since you are modifying the global variable.
For example:
You would get:
alert of “global: 0” from alert_global()
alert of “global: 1” from local_inc()
alert of “local: 2” from local_inc()
alert of “global: 1” from alert_global()