2

ich want to check if my 3 variables are defined but this doesnt work:

If (typeof inputServer && inputUser && inputPassword !="undefined") {
        alert("it works!");
    }

My debugger says "Uncaught SyntaxError: Unexpected token { "

Please help me :)

greets Tom

user2219190
  • 157
  • 2
  • 13

4 Answers4

4

JavaScript is case sensitive language. if keyword starts with small i.

Additionally, I'd recommend you to use typeof inputServer !== "undefined" to check if variable was defined.

REF: Detecting an undefined object property

Community
  • 1
  • 1
VisioN
  • 143,310
  • 32
  • 282
  • 281
3
if ((inputServer != undefined) && (inputUser != undefined) && (inputPassword != undefined)) {
        alert("it works!");
}
zavg
  • 10,351
  • 4
  • 44
  • 67
0

it's if(all lower) and not If - and if the vars is not defined it's still gives you an error cause you are asking them - what you can do is (assuming this code runs at global scope as i understand:

if(typeof(window.inputServer) != 'undefined' && typeof(window.inputUser) != 'undefined'){

}
Adidi
  • 5,097
  • 4
  • 23
  • 30
0

First, if should be lowercase.

Then, you could do it like this:

if (inputServer !== undefined && inputUser !== undefined && inputPassword !== undefined) {
    alert("it works!");
}

If you wanted to check if they were not undefined OR null you could do (and this is one of the rare circumstances when using double equality is useful):

if (inputServer != null && inputUser != null && inputPassword != null) {
    alert("it works!");
}

And lastly, if you wanted to check if they weren't any falsy value (null, undefined, false, -0, +0, NaN, ''), you could do:

if (inputServer && inputUser && inputPassword) {
    alert("it works!");
}

Sidenote: You don't need to use typeof in most normal circumstances -- that is unless undefined is actually a variable defined somewhere in your program...

Fortunately, in ECMAScript 5.1 it's not possible to overwrite window.undefined http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.1.3

holographic-principle
  • 19,688
  • 10
  • 46
  • 62