0

I am setting a variable in an if...else statement. However, when I try to call the variable in another function, I get the error that the variable is not defined. How can I set a global variable?

function username_check(){  
username = $('#username').val();
if(username == "" || username.length < 7 || username.indexOf(' ') > -1){
    usernameFilled = 0;
else{
    usernameFilled = 1;
}   
}

function email_check(){ 
email = $('#email').val();
if(email == "" || email.indexOf(' ') > -1) {
    emailFilled = 0;
}else{
    emailFilled = 1;
}
}

function password_length(){ 
password = $('#password').val();
if(password == "" || password.indexOf(' ') > -1) {
    passwordFilled = 0;
}else{
    passwordFilled = 1;
}
}

function password_check(){  
  password2 = $('#password2').val();
  password = $('#password').val();
  if(password2.length > 7 && password2 == password) {
    password2Filled = 1; /**setting the variable**/
  }else{
    password2Filled = 0;
  }
}

function upload(){
if (usernameFilled == 0 || emailFilled == 0 || passwordFilled == 0 || password2Filled == 0) {
    alert('All fields are required');
}else{
    /**upload to database**/
}
17
  • Please reproduce your issue on jsfiddle Commented Jul 4, 2012 at 2:47
  • 1
    are you sure you're calling upload after you call password_check ? Commented Jul 4, 2012 at 2:49
  • @Tats_innit: global variable shouldn't be declared in a special way. It's declaration goes on first initialization entry Commented Jul 4, 2012 at 2:49
  • 3
    @Tats_innit: - var is what differs local variables from global ones. And the question is about globals. If you don't specify var - the variable becomes global, and a = 1; equals to window.a = 1; Commented Jul 4, 2012 at 2:53
  • 1
    @mr.musicman then do the same for the calls to these functions (copy-paste the calls to password_check and upload! Commented Jul 4, 2012 at 2:56

3 Answers 3

1

Rather than setting a global variable just return password2Filled and save it outside the function. Then you can pass it into the next function.

ie

function password_check(){  
   password2 = $('#password2').val();
   password = $('#password').val();
   if(password2.length > 7 && password2 == password) {
       password2Filled = 1; /**setting the variable**/
   }else{
       password2Filled = 0;
   }
   return password2Filled;
}

function upload(password2Filled)
{
    if (password2Filled == 0) { /**calling the variable**/
        alert('All fields are required');
    }else{
        /**upload to database**/
    }
}

....

var passwordsOk = password_check();
upload(passwordOk);

Try and avoid global variables, they clutter up the program and make it difficult to see the flow of the code and to create code that is reusable.

Sign up to request clarification or add additional context in comments.

1 Comment

i did try that. Might have done something wrong in there, I just kept getting the error that the variables weren't defined. I'll have to try it again...
1

You're probably calling the functions in the wrong order, or doing something else wrong, cause it works just fine for me, edited the code slightly to test it:

function password_check(){  
  var password2 = $('#password2').val(), 
      password = $('#password').val();
  password2Filled = (password2.length > 7 && password2 == password) ? 1:0;
}

function upload(){
    console.log(password2Filled); //prints 0 or 1 just fine ???
}

$("#btn").on('click', function() {
    password_check();
    upload();
});

FIDDLE

1 Comment

It's true, I am trying to call more functions than just one. I'll update my question to show you. Maybe you can spot the error. Thanks!
0

You create global variables in JavaScript by defining them outside of the scope of all functions. Like so:

<script type="text/javascript">
var globalVariable;


function blah()
{
    globalVariable = "something";
}

function anotherFunction()
{
    alert(globalVariable);
}
</script>

The ECMAScript / JavaScript documentation states that a "Global Object" should be created outside of any execution context.

7 Comments

Actually, you declare global variables by using the window object, like window.myVar = something, and just skipping the 'var' keyword will do that no matter what scope it's in.
@adeneo Do you have any official documentation or sources to cite that instruct you to use that method? Just because it works doesn't mean it's a documented best practice.
Nope, but unless you're using ECMA 5 in strict mode it will work just fine, but there's nothing wrong with declaring variables, and it is both good practice and recommended as far as I know, but failing to declare in the global scope is almost certainly not the problem in this case.
Actually, the MDN documentation is here! -- Using var outside a function is optional; assigning a value to an undeclared variable implicitly declares it as a global variable (also a property of the global object). The difference is that a declared variable is a non-configurable property of the global object while an undeclared is configurable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.