Dave's Brain

Browse - Programming Tips - Is there a robust way to check a string for being empty?

Date: 2014nov16
Updated: 2017jun8
Language: javaScript

Q.  Is there a robust way to check a string for being empty?

A.  These functions handle undefined, null, zero length and ignore leading and trailing space

function isSpace(s) { if (s === undefined || a == null) return true; if (typeof s != 'string') s = s.toString(s); if (s.length == 0) return true; return s.match(/^\s*$/); } function hasContent(s) { return ! isSpace(s); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.