I set the value of a hidden field #thimble
on page load using server-side values.
Then in JavaScript I want to act on that value only if it has been populated with some non-empty string.
Is this the most concise way of checking that the value is non-empty?
if ($("#thimble").val() != null && $("#thimble").val().length > 0) {
carryOn();
}
Seems rather long.
An empty string is a falsey value, I wouldn't even bother to check its length
.
The following is equivalent to your example:
if ($("#thimble").val()) {
carryOn();
}
A falsey value is a value that produces false
when evaluated in Boolean context (such as the condition of an if
statement).
Falsey values are:
null
undefined
NaN
0
""
(empty string)false
Remember that a string in Boolean context produces false
only when its length is 0
, if it has whitespace it still produce true
:
Boolean(""); // false
Boolean(" "); // true, whitespace
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With