Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use var? [duplicate]

Possible Duplicate:
Javascript: is using ‘var’ to declare variables optional?

how come in this code example below when i put var before the variables it does not work, however if i remove the var it works? i thought you had to use var when you were creating new variables.

function myfunction () {
    if (document.getElementById('ramyes').checked) {
      var itischecked = "yes"
    } else if (document.getElementById('ramno').checked) {
      var itischecked = "no"
    }
}   



function display () {
  myfunction()

  if (itischecked == "yes") {
    alert ("it sure is");
  } else if (itischecked == "no") {
    alert ("it is not");
  }
}
like image 375
Ryan Conway Avatar asked Jan 30 '26 18:01

Ryan Conway


2 Answers

If you use var, the variable is only visible inside the current function (it's a local variable). If you don't use var when first setting the variable, it creates a global variable, which is visible to all functions.

Defining global variables by just setting them is usually frowned upon because most of the time you want local variables (in your case you should return your "itischecked" value from the function, instead of storing it in a global variable), and setting a variable without var looks like a mistake. It is also an error in strict mode (which everybody should use, always). To explicitly define a global variable in (browser) JS, use window.variableName, or var in the global scope (outside any functions).

But before you do that, think carefully whether that's a good idea. The more global variables you have, the higher the chance you will get a name collision when multiple scripts use the same variable name.

like image 66
Matti Virkkunen Avatar answered Feb 02 '26 08:02

Matti Virkkunen


You should use var when you create a variable, but if you want to use the same variable in two functions, the variable has to be created in a scope outside both functions:

var itischecked;

function myfunction () {
  if(document.getElementById('ramyes').checked) {
    itischecked = "yes"
  } else if(document.getElementById('ramno').checked) {
    itischecked = "no"
  }
}   

function display () {
  myfunction();
  if (itischecked == "yes") {
    alert ("it sure is");
  } else if (itischecked == "no") {
    alert ("it is not");
  }
}

If you create a variable inside a function, that becomes a local variable. It is only visible inside that function, and goes away when you return from the function.


In your case you should rather use the return value of the function instead of putting a value in a variable. That way it is easier to follow how the data flows in the program, and you don't need to create a global variable:

function myfunction () {
  if(document.getElementById('ramyes').checked) {
    return "yes"
  } else if(document.getElementById('ramno').checked) {
    return "no"
  }
}   

function display () {
  var itischecked = myfunction();
  if (itischecked == "yes") {
    alert ("it sure is");
  } else if (itischecked == "no") {
    alert ("it is not");
  }
}
like image 30
Guffa Avatar answered Feb 02 '26 07:02

Guffa