In Rails we can .present?
to check if a string is not-nil
and contains something other than white-space or an empty string:
"".present? # => false
" ".present? # => false
nil.present? # => false
"hello".present? # => true
I would like similar functionality in Javascript, without having to write a function for it like function string_present?(str) { ... }
Is this something I can do with Javascript out-of-the-box or by adding to the String
's prototype?
I did this:
String.prototype.present = function()
{
if(this.length > 0) {
return this;
}
return null;
}
But, how would I make this work:
var x = null; x.present
var y; y.present