Checking if a string is empty in JavaScript. Idiomatic or readable?

2

I understand that it is often best to appreciate the legibility and clarity of code by programming explicitly. This does not stop at JavaScript, but also at C # , Python and perhaps the rest of the programming languages.

I know these conventions are not rules , but they help the development process if they are chosen at the right time , in the right environment and in the right way.

In JavaScript to test whether a string is empty or not, you can use:

const value = '';
if(!value) {
    console.log('String vazia!');
}

This happens because the empty string is a value set to falsy ( documentation ). And besides comparing value with "" , it compares with null and several other values.

While working, does not this form of readability impair readability? It is not clear to a C # or Java developer that this if is the condition of an empty string.

In this case, comparing explicitly would hurt a JavaScript idiom, why?

    
asked by anonymous 23.09.2017 / 12:27

2 answers

8

If the intent is to verify that the string is empty , then yes impairs code reading and possibly implementation. The reason is that the comparison made in:

if (!value) {
    console.log("String vazia");
}

is not a check if the string is empty, but rather if the value of value is falsy , as commented in the question itself. This implies that value , besides can not be an empty string , can not be the integer zero, false, null, undefined, NaN not a number etc. However, all these values would be expected to pass the "not an empty " test because they are not, in fact, an empty string (except the string empty itself). When I do if (!value) , it should be understood that value can be any truthy value and not just a nonempty string .

const testes = ["", false, null, undefined, NaN, [], 0, "Foo"];

for (let teste of testes) {
  if (!teste) {
    console.log("String vazia: " + teste);
  } else {
    console.log("Não é uma string vazia: " + teste);
  }
}

To actually check that value is an empty string , you will have to check the type of the variable, either using typeof :

const testes = ["", false, null, undefined, NaN, [], 0, "Foo"];

for (let teste of testes) {
  if (typeof teste === "string" && teste.length == 0) {
    console.log("String vazia: " + teste);
  } else {
    console.log("Não é uma string vazia: " + teste);
  }
}

Note that when checking the type it is explicit that it is really desired to search for empty strings, so all other values are valid.

That is, using the hard comparison, with the === operator, since == makes a loose comparison.

What is a loose comparison?

const testes = ["", false, null, undefined, NaN, [], 0, "Foo"];

for (let teste of testes) {
  if (teste === "") {
    console.log("String vazia: " + teste);
  } else {
    console.log("Não é uma string vazia: " + teste);
  }
}

Again, the check becomes explicit about your goal.

  

It's very important to note that null is different from empty string . The Maniero's answer on this graphically shows the difference.

    
23.09.2017 / 14:14
1

You can use class String to start a new variable of this type, see:

const myText = new String("Hello!");
console.log(myText.length); // Saída: 6 (tipo número)

const emptyText = new String("");
console.log(emptyText.length); // Saída: 0 (tipo número)

You can check the compatibility of this feature in the MDN website .

    
23.09.2017 / 13:41