{"id":1999,"date":"2017-03-24T11:20:48","date_gmt":"2017-03-24T05:50:48","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1999"},"modified":"2017-03-28T16:44:16","modified_gmt":"2017-03-28T11:14:16","slug":"check-empty-string-javascript","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/check-empty-string-javascript\/","title":{"rendered":"[ Solved &#8211; 12 Answers ] JAVASCRIPT &#8211; How to check for an empty string in JavaScript"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM<\/label><\/p>\n<ul>\n<li>How to check for an empty string in JavaScript<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201djavascript%20null%20is-empty%0A%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<ul>\n<li>If we need to check whether there\u2019s any value in string,<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif%20(strValue)%20%7B%0A%20%20%20%20%2F%2Fdo%20something%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li style=\"top: 90px;\">If we need to check specifically for an empty string over null, we would think checking against \u201c\u201d is our best, using the === operator (In fact, a string we are comparing against).<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif%20(!a)%20%7B%0A%20%20%2F%2F%20is%20emtpy%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>To ignore white space for strings:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif%20(!a.trim())%20%7B%0A%20%20%20%20%2F%2F%20is%20empty%20or%20whitespace%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li>If we need legacy support (IE8-) for trim(), use $.trim or a polyfill.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<ul>\n<li>check if the variable has a truthy value or not. That means<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif(%20value%20)%20%7B%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 90px;\">will evaluate to true if value is not:<\/p>\n<ul>\n<li>null<\/li>\n<li>undefined<\/li>\n<li>NaN<\/li>\n<li>empty string (\u201c\u201d)<\/li>\n<li>0<\/li>\n<li>false<\/li>\n<\/ul>\n<p>The above list represents all possible false values in ECMA-\/Javascript. Find it in the specification at the ToBoolean section.<br \/>\nFurthermore, if we do not know whether a variable exists (that means, if it was declared) then we should check with the typeof operator. For instance<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif(%20typeof%20wiki!%3D%3D%20\u2019undefined\u2019%20)%20%7B%0A%20%20%20%20%2F%2F%20wiki%20could%20get%20resolved%20and%20it\u2019s%20defined%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li style=\"top: 90px;\">Make sure that a variable is declared at least, we should directly check if it has a truthy value<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dfunction%20isEmpty(str)%20%7B%0A%20%20%20%20return%20(!str%20%7C%7C%200%20%3D%3D%3D%20str.length)%3B%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>If a string is blank, null or undefined then we can use the following:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dfunction%20isBlank(str)%20%7B%0A%20%20%20%20return%20(!str%20%7C%7C%20%2F%5E%5Cs*%24%2F.test(str))%3B%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li style=\"top: 90px;\">If a string is blank or contains only white-space:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dString.prototype.isEmpty%20%3D%20function()%20%7B%0A%20%20%20%20return%20(this.length%20%3D%3D%3D%200%20%7C%7C%20!this.trim())%3B%0A%7D%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<ul>\n<li>Using !!(not not) operator:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif(!!str)%7B%0Asome%20code%20here%3B%0A%7D\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 90px;\">or<\/p>\n<h4 id=\"using-type-casting\"><strong><span style=\"color: #808000;\">Using type casting:<\/span><\/strong><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif(Boolean(str))%7B%0A%20%20%20%20codes%20here%3B%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li>Both do the same function, type cast the variable to boolean, where str is a variable.<\/li>\n<li>Returns false for null,undefined,0,000,\u201d\u201d,false.<\/li>\n<li>Returns true for string \u201c0\u201d and whitespace \u201d \u201c.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<ul>\n<li>Make sure that the string is not just a bunch of empty spaces (we are assuming this is for form validation) we need to replace on the spaces.<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif(str.replace(%2F%5Cs%2Fg%2C%22%22)%20%3D%3D%20%22%22)%7B%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<ul>\n<li>We can use the following Solution:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dfunction%20empty(e)%20%7B%0A%20%20switch%20(e)%20%7B%0A%20%20%20%20case%20%22%22%3A%0A%20%20%20%20case%200%3A%0A%20%20%20%20case%20%220%22%3A%0A%20%20%20%20case%20null%3A%0A%20%20%20%20case%20false%3A%0A%20%20%20%20case%20typeof%20this%20%3D%3D%20%22undefined%22%3A%0A%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20default%3A%0A%20%20%20%20%20%20return%20false%3B%0A%20%20%7D%0A%7D%0A%0Aempty(null)%20%2F%2F%20true%0Aempty(0)%20%2F%2F%20true%0Aempty(7)%20%2F%2F%20false%0Aempty(%22%22)%20%2F%2F%20true%0Aempty((function()%20%7B%0A%20%20%20%20return%20%22%22%0A%20%20%7D))%20%2F%2F%20true%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 8:<\/label><\/p>\n<ul>\n<li>Several methods:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%2F%2Fwhen%20undefined%0Aif%20(typeof%20MyVariable%20%3D%3D%20\u2019undefined\u2019)%0A%0A%2F%2Fwhen%20false%0Aif%20(MyVariable%20%3D%3D%20false)%20%2F%2Fsame%20as%20if(!MyVariable%20)%0A%0A%2F%2Fwhen%20defined%2C%20but%20empty%0Aif%20(%0A%20%20%20%20(MyVariable.length%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%7C%0A%20%20%20%20(MyVariable%20%3D%3D%20%22%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%7C%0A%20%20%20%20(MyVariable.replace(%2F%5Cs%2Fg%2C%22%22)%20%3D%3D%20%22%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%7C%0A%20%20%20%20(!%2F%5B%5E%5Cs%5D%2F.test(MyVariable))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%7C%0A%20%20%20%20(%2F%5E%5Cs*%24%2F.test(MyVariable))%0A%20%20%20)%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 9:<\/label><\/p>\n<ul>\n<li>Here we can use the regexps:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif((%2F%5E%5Cs*%24%2F).test(str))%20%7B%20%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li style=\"top: 90px;\">Checks for strings that are either empty or filled with whitespace.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 10:<\/label><\/p>\n<ul>\n<li>For example, if we have a null character string:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dvar%20y%20%3D%20%22%5C0%22%3B%20%2F%2F%20an%20empty%20string%2C%20but%20has%20a%20null%20character%0A(y%20%3D%3D%3D%20%22%22)%20%2F%2F%20false%2C%20testing%20against%20an%20empty%20string%20does%20not%20work%0A(y.length%20%3D%3D%3D%200)%20%2F%2F%20false%0A(y)%20%2F%2F%20true%2C%20this%20is%20also%20not%20expected%0A(y.match(%2F%5E%5B%5Cs%5D*%24%2F))%20%2F%2F%20false%2C%20again%20not%20wanted%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>To test its nullness, we can do the following:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dString.prototype.isNull%20%3D%20function()%7B%20%0A%20%20return%20Boolean(this.match(%2F%5E%5B%5C0%5D*%24%2F))%3B%20%0A%7D%0A\u2026%0A%22%5C0%22.isNull()%20%2F%2F%20true%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 90px;\">It works on a null string, and on an empty string and it is accessible for all strings. In addition, it could be expanded to contain other JavaScript empty or whitespace characters (i.e. nonbreaking space, byte order mark, line\/paragraph separator, etc.).<\/p>\n<p><label class=\"label label-info\">SOLUTION 11:<\/label><\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%09%0A%0Afunction%20isBlank(pString)%7B%0A%20%20%20%20if%20(!pString%20%7C%7C%20pString.length%20%3D%3D%200)%20%7B%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%7D%0A%20%20%20%20%2F%2F%20checks%20for%20a%20non-white%20space%20character%20%0A%20%20%20%20%2F%2F%20which%20I%20think%20%5Bcitation%20needed%5D%20is%20faster%20%0A%20%20%20%20%2F%2F%20than%20removing%20all%20the%20whitespace%20and%20checking%20%0A%20%20%20%20%2F%2F%20against%20an%20empty%20string%0A%20%20%20%20return%20!%2F%5B%5E%5Cs%5D%2B%2F.test(pString)%3B%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 12:<\/label><\/p>\n<ul>\n<li>Starting with:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dreturn%20(!value%20%7C%7C%20value%20%3D%3D%20undefined%20%7C%7C%20value%20%3D%3D%20%22%22%20%7C%7C%20value.length%20%3D%3D%200)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li style=\"top: 90px;\">Looking at the last condition, if value == \u201c\u201d, it\u2019s length MUST be 0. Therefore drop it:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dreturn%20(!value%20%7C%7C%20value%20%3D%3D%20undefined%20%7C%7C%20value%20%3D%3D%20%22%22)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>In JS, an empty string is false. Therefore, drop value == \u201c\u201d:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dreturn%20(!value%20%7C%7C%20value%20%3D%3D%20undefined)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li style=\"top: 90px;\">And !undefined is true, so that check isn\u2019t needed. So we have:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dreturn%20(!value)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li style=\"top: 90px;\">And we don\u2019t need parentheses:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dreturn%20!value%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM How to check for an empty string in JavaScript [pastacode lang=\u201djavascript\u201d manual=\u201djavascript%20null%20is-empty%0A%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] SOLUTION 1: If we need to check whether there\u2019s any value in string, [pastacode lang=\u201djavascript\u201d manual=\u201dif%20(strValue)%20%7B%0A%20%20%20%20%2F%2Fdo%20something%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] If we need to check specifically for an empty string over null, we would think checking against \u201c\u201d [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275],"tags":[4265,4273,4268,4258,4267,4262,4257,4269,4271,4274,4270,4275,4256,4272,4276,4266,4260,4263,4255,4261,4264,4259],"class_list":["post-1999","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-1-6-checking-for-an-existing","tag-check-if-string-is-empty-java","tag-how-to-check-if-javascript-object-is-empty-example","tag-is-there-a-standard-function-to-check-for-null","tag-isnan-javascript","tag-javascript-falsy-values-vs-null","tag-javascript-what-is-the-best-way-to-test-for-an-empty-string-with-jquery","tag-javascript-check-for-null-or-empty-string","tag-javascript-check-if-string-is-empty-or-whitespace","tag-javascript-create-empty-string","tag-javascript-is-empty-array","tag-javascript-is-empty-object","tag-javascript-how-to-check-if-a-string-is-empty","tag-jquery-empty-string","tag-lodash-empty-string","tag-nonempty-string","tag-or-blank","tag-or-empty-string","tag-related-queries-null-how-do-you-check-for-an-empty-string-in-javascript","tag-string-isnullorempty-in-javascript","tag-test-for-empty-values-in-javascript","tag-undefined"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1999"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1999\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}