How can I safely decide if a variable is a string of more than one characters?

Posted by Alan on Stack Overflow See other posts from Stack Overflow or by Alan
Published on 2013-11-10T09:43:25Z Indexed on 2013/11/10 9:53 UTC
Read the original article Hit count: 191

Filed under:

I am using the following Javascript:

 if (typeof content !== 'undefined' && content.length > 0) {
    $state.transitionTo('admin.content', { content: content })
 }

I thought this was safe to use but it gives me an error saying:

TypeError: Cannot read property 'length' of null

I am using the following function to decide if something is a number:

    isNumber: function (num) {
        // Return false if num is null or an empty string
        if (num === null || (typeof num === "string" && num.length === 0)) {
            return false;
        }
        var rtn = !isNaN(num)
        return rtn;

    },

How can I write a similar function that would very safely determine if something is a string with a length of more than 0?

© Stack Overflow or respective owner

Related posts about JavaScript