I want to try to do string call equivalent to the C# String.IsNullOrEmpty(string)
in javascript. I looked online assuming that there was a simple call to make, but I could not find one.
For now I am using a if(string === "" || string === null)
statement to cover it, but I would rather use a predefined method (I keep getting some instances that slip by for some reason)
What is the closest javascript (or jquery if then have one) call that would be equal?
You're overthinking. Null and empty string are both falsey values in JavaScript.
if(!theString) { alert("the string is null or empty"); }
Falsey:
If, for whatever reason, you wanted to test only null
and empty
, you could do:
function isNullOrEmpty( s ) { return ( s == null || s === "" ); }
Note: This will also catch undefined as @Raynos mentioned in the comments.
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