2016年6月21日 星期二

String.IsNullOrEmpty in JavaScript

Starting with:
return (!value || value == undefined || value == "" || value.length == 0);
Looking at the last condition, if value == "", it's length MUST be 0. Therefore drop it:
return (!value || value == undefined || value == "");
But wait! In JS, an empty string is false. Therefore, drop value == "":
return (!value || value == undefined);
And !undefined is true, so that check isn't needed. So we have:
return (!value);
And we don't need parentheses:
return !value
Q.E.D.


http://codereview.stackexchange.com/questions/5572/string-isnullorempty-in-javascript

沒有留言:

張貼留言