Sign in to follow this  

JavaScript Help

Recommended Posts

Declare it outside the function. Just make sure you don't re-declare is inside the function, else it will create a variable with local scope with the same name.

// Right
var myvar = "mystring";
function myfunc() {
myvar = "changed"; // this changes the global

// Wrong
var myvar = "mystring";
function myfunc() {
var myvar = "changed"; // Doh! We've just created a local

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this