Почему я должен использовать string.length == 0 по строке == “” при проверке на пустую строку в ECMAScript?

Быстрый ответ: Добавьте конфигурацию прокси с параметром для обеих установок/обновлений

gem install --http-proxy http://host:port/ package_name

gem update --http-proxy http://host:port/ package_name
12
задан Shog9 17 August 2010 в 23:39
поделиться

2 ответа

I actually prefer that technique in a number of languages, since it's sometimes hard to differentiate between an empty string literal "" and several other strings (" ", '"').

But there's another reason to avoid theString == "" in ECMAScript: 0 == "" evaluates to true, as does false == "" and 0.0 == ""...

...so unless you know that theString is actually a string, you might end up causing problems for yourself by using the weak comparison. Fortunately, you can avoid this with judicious use of the strict equal (===) operator:

if ( theString === "" )
   // string is a string and is empty

See also:

29
ответ дан 2 December 2019 в 04:53
поделиться

The problem is that if theString is set to 0 (zero) your 2nd example will evaluate to true.

1
ответ дан 2 December 2019 в 04:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: