4
var characterName = document.getElementById("pilotName").value;
var characterID = 0;
var charSecStatus = 0;
var corpName = " ";
var allianceName = " ";


//callback
makeRequest('https://api.eveonline.com/eve/CharacterID.xml.aspx?names=' + characterName, function() {
  if (xmlhttp.status == OK_RESPONSE) {

    //read character info
    characterID = xmlhttp.responseXML.getElementsByTagName("row")[0].getAttribute("characterID");

    makeRequest('https://api.eveonline.com/eve/CharacterInfo.xml.aspx?characterID=' + characterID, function() {

      if (xmlhttp.status == OK_RESPONSE) {

        //read character info 
        characterID = xmlhttp.responseXML.getElementsByTagName("characterID")[0].innerHTML;
        charSecStatus = xmlhttp.responseXML.getElementsByTagName("securityStatus")[0].innerHTML;
        corpName = xmlhttp.responseXML.getElementsByTagName("corporation")[0].innerHTML;
        allianceName = (xmlhttp.responseXML.getElementsByTagName("alliance")[0] || {
          innerHTML: ""
        }).innerHTML;
      }
    });
  }
});

(partial code pictured, no bracketspam pls) I'm trying to check if the "alliance" variable is empty because certain '"corp" are not in "alliances" and it would be critical error on website if it tried to display an empty variable, so is there a way to check if allianceName is empty after retrieving it from the XML tree and setting it to like "No Alliance" if it IS empty? Thanks

simhumileco
  • 31,877
  • 16
  • 137
  • 115
timoxazero
  • 41
  • 1
  • 1
  • 2
  • 1
    Possible duplicate of [How do you check for an empty string in JavaScript?](http://stackoverflow.com/questions/154059/how-do-you-check-for-an-empty-string-in-javascript) – PM 77-1 Feb 10 '16 at 04:11
  • Those answers will not work. The `allianceName` variable he gave was given a non-empty value `" "`. In this case, the value of that variable is actually true. A string with whitespace characters has a true value – Richard Hamilton Feb 10 '16 at 04:16
  • @RichardHamilton so I need to initialize it as var allianceName = ""? – timoxazero Feb 10 '16 at 04:19
  • yep, it's a common mistake in JavaScript – Richard Hamilton Feb 10 '16 at 04:19
  • @timoxazero—you don't need to initialise it at all, just declare it and it will be given a value of *undefined*, which is falsey. – RobG Feb 10 '16 at 04:31

4 Answers4

3

You are declaring true variables here

var corpName = " ";
var allianceName = " "

In JavaScript, the value of a string with whitespace characters is actually truthy. While the above values do look empty, the reality is they're not. Try it out yourself.

Boolean(" ");
=> true

To set these variables empty, you need to declare them without whitespace characters

var corpName = "";
var allianceName = "";

After you have this, there are two ways to check if the variable is empty

if (!corpName || !corpName.length)

Assuming, you did accidentally include the whitespace character, there are several ways to fix this. We can modify these strings in these cases.

Boolean(" ".trim());
=> false

In the above example, the trim() function is used to remove whitespace characters from a string.

You could also search the string for non-whitespace characters

/\S/.test(" ")
=> false

The \S modifier finds all non whitespace characters.

Alternatively, you could also use the replace function

Boolean(" ".replace(/\s/g, ''));

The global flag g finds all references to the previous regular expression.

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
  • Perhaps pedantic, but [*trim*](http://www.ecma-international.org/ecma-262/6.0/) only removes leading and trailing white space, and the set of [white space (or *WhiteSpace*) characters](http://www.ecma-international.org/ecma-262/6.0/#sec-white-space) has 7 members, not just the space character. Worth an up vote all the same. :-) – RobG Feb 10 '16 at 04:29
  • For strings to check if empty this worked better for me: `Boolean(" ".trim())` – csalmeida Feb 09 '23 at 20:51
0

If you just want to check whether there's any value in a variable, then try this,

if (str) {
    //do something
}

or,

//when undefined
if (typeof str == 'undefined'){
//do something
}

If you need to check specifically for an empty string over null, then, try this.

//when defined, but empty
if ((str.length == 0)||(str== ""))
{
//do something
}
itzmebibin
  • 9,199
  • 8
  • 48
  • 62
0

You can try something like this:

Note: If you value is null or undefined, !value?true:false should work. Also value.length is also good approach but should be used in case of string. If value is number, value.length will return 0, i.e. true for empty check.

function isEmpty(value) {
  switch (typeof(value)) {
    case "string": return (value.length === 0);
    case "number":
    case "boolean": return false;
    case "undefined": return true;
    case "object": return !value ? true : false; // handling for null.
    default: return !value ? true : false
  }
}

var a = " ";
console.log(a, "is empty:", isEmpty(a))
console.log(a, "is empty:", isEmpty(a.trim()))

a = 0;
console.log(a, "is empty:", isEmpty(a))

a = undefined;
console.log(a, "is empty:", isEmpty(a))

a = null;
console.log(a, "is empty:", isEmpty(a))

Also, you can change

allianceName = (xmlhttp.responseXML.getElementsByTagName("alliance")[0] || {
   innerHTML: "
}).innerHTML;

to

allianceName = xmlhttp.responseXML.getElementsByTagName("alliance")[0].innerHTML || "";
Rajesh
  • 24,354
  • 5
  • 48
  • 79
-1

I believe in JavaScript there's a function called isset() which takes one argument and returns true if it is set to a value and false if it is not.

Zackeezy
  • 74
  • 8
  • 1
    Reality says otherwise… all declared javascript variables are initialised to *undefined*, so they always have a value, whether one was assigned or not. ;-) – RobG Feb 10 '16 at 04:21
  • Oh yea, that's right. I learned both at the same time so sometimes they blend together. – Zackeezy Feb 10 '16 at 04:54